Android device has advance function,It has hardware to detect shake event.It can also detect the direction of shaking device.This advance function measure the value of X, Y and Z direction.Screenshot of Shake Listener is given below.
How to Develop ?
- Create one Project named "ShakeListener".
- Insert below code in activity_main.xml file which is stored in res/layout directory of project folder.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0|1">
<TableRow>
<TextView
android:text="X :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text="Y :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text="Z :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text="aX :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/ax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text="aY :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/ay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text="aZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/az"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
</TableRow>
</TableLayout>
</LinearLayout>
- Now Open MainActivity.java file and insert Following code in it.
MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager = null;
private TextView x, y, z, ax, ay, az;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.activity_main);
// initialize display
x = (TextView) findViewById(R.id.x);
y = (TextView) findViewById(R.id.y);
z = (TextView) findViewById(R.id.z);
ax = (TextView) findViewById(R.id.ax);
ay = (TextView) findViewById(R.id.ay);
az = (TextView) findViewById(R.id.az);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(SensorManager.SENSOR_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
super.onStop();
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(SensorManager.SENSOR_ORIENTATION));
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case SensorManager.SENSOR_ORIENTATION :
x.setText(String.valueOf(event.values[SensorManager.DATA_X]));
y.setText(String.valueOf(event.values[SensorManager.DATA_Y]));
z.setText(String.valueOf(event.values[SensorManager.DATA_Z]));
break;
case SensorManager.SENSOR_ACCELEROMETER :
ax.setText(String.valueOf(event.values[SensorManager.DATA_X]));
ay.setText(String.valueOf(event.values[SensorManager.DATA_Y]));
az.setText(String.valueOf(event.values[SensorManager.DATA_Z]));
break;
}
}
}
}
- If you get few errors then press CTRL+SHIFT+O. It imports all missing libraries.
- That's It. Now run the project.
No comments:
Post a Comment