INTRODUCTION
In this lesson you will be getting access to your accelerometer. And being able to read the x, y, and z values that it gives it.
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Access your accelerometer
2. Be able to read what the accelerometer is reading
3. Be able to set up a Sensor and SensorManager Activity
LEARNING SEQUENCE
|
|
Required Reading
|
Read the following:
Introduction To Development Environment
· Eclipse
· Android
· Considerations for beginning programming.
|
Resources
|
View the following:
1. Eclipse
2. Android Development
|
Assignments
|
We’ll briefly hit the points for the XML layout in this one, you should be good with TextViews.
Create a new XML document. We called it ‘accel’
If it isn’t in ‘RelativeLayout’, then change the ‘LinearLayout’.
Change the width and height to ‘match_parent’, and add ‘android:id=”@+id/relative” ‘ at the bottom.
We’ll have 4 TextViews in this app. Make the first of them a slightly bigger text size than the others, because it will be the title. Name them like we do. 1. ‘android:id=”@+id/name” ‘ 2. ‘android:id=”@+id/xval” ‘ 3. ‘android:id=”@+id/yval” ‘ 4. ‘android:id=”@+id/zval” ‘
Make the width ‘fill_parent’ and the height ‘wrap_content’. Also, add 2.‘android:layout_below=”@+id/name” ‘ 3.‘android:layout_below=”@+id/xval” ‘ 4.‘android:layout_below=”@+id/yval” ‘ making them show up in that order
Now for the Java. Start a new class, name it Accelerometer. Type ‘extends Activity implements SensorEventListener’ within the brackets. Import the Activity and SensorEventListener. And add the unimplemented methods of the Accelerometer.
Now after the brackets, type ‘private SensorMangager mSensorManager;’
Enter, ‘private Sensor mAccelerometer;
Enter, ‘TextView title,tv, tv1,tv2;’
Enter, ‘RelativeLayout layout;’
Import ‘onCreate(Bundle)’ like you did in lesson 9.
Below the ‘super.onCreate’ type, ‘setContentView(R.layout.accel);’
Enter ‘mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- Enter ‘mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);’
|
No comments:
Post a Comment