Get detail info of Accelerometer
package com.exercise.AndroidAccelerometer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AndroidAccelerometerActivity extends Activity
implements SensorEventListener{
private SensorManager sensorManager;
private Sensor sensorAccelerometer;
TextView readingAzimuth, readingPitch, readingRoll;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer = sensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER);
readingAzimuth = (TextView)findViewById(R.id.azimuth);
readingPitch = (TextView)findViewById(R.id.pitch);
readingRoll = (TextView)findViewById(R.id.roll);
TextView info = (TextView)findViewById(R.id.info);
String stringInfo = "\n"
+ "Name: " + sensorAccelerometer.getName() + "\n"
+ "Type: " + sensorAccelerometer.getType() + "\n"
+ "Vendor: " + sensorAccelerometer.getVendor() + "\n"
+ "Version: " + sensorAccelerometer.getVersion() + "\n"
+ "Class: " + sensorAccelerometer.getClass() + "\n"
+ "Resolution: " + sensorAccelerometer.getResolution() + "\n"
+ "MaximumRange: " + sensorAccelerometer.getMaximumRange() + "\n"
+ "Power: " + sensorAccelerometer.getPower() + "\n";
info.setText(stringInfo);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
/*
* event.values[0]: azimuth, rotation around the Z axis.
* event.values[1]: pitch, rotation around the X axis.
* event.values[2]: roll, rotation around the Y axis.
*/
float valueAzimuth = event.values[0];
float valuePitch = event.values[1];
float valueRoll = event.values[2];
readingAzimuth.setText("Azimuth: " + String.valueOf(valueAzimuth));
readingPitch.setText("Pitch: " + String.valueOf(valuePitch));
readingRoll.setText("Roll: " + String.valueOf(valueRoll));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/azimuth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/pitch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/roll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Related:
- SurfaceView Game step-by-step: react device movement/orientation using accelerometer
0 komentar:
Posting Komentar