1
因为代码很好理解,所以基本每篇文章都是上代码,很少解释,我想应该一看就会吧
package com.example.d;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
public class ZhongLiActivity extends Activity{
float x,y,z=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.sensor);
final TextView text = (TextView)this.findViewById(R.id.show);
System.out.println(text);
// Log.i("text", text.toString());
SensorManager manager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
text.setText("X:"+x+",Y:"+y+",Z:"+z);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
manager.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_GAME);
}
}
2