我是 android 的初学者,在以下示例中使用 android 打印陀螺仪值时遇到问题。
public class Gyro extends Activity implements OnClickListener,SensorEventListener {
Button click;
SensorManager sm;
Sensor gyroscope;
int decide=1;
Context c;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gyro);
c = getApplicationContext();
click = (Button)findViewById(R.id.btnAction);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
gyroscope = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
txt = (TextView)findViewById(R.id.txt) ;
click.setText("Start");
click.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
if(decide==1)
{
sm.registerListener(Gyro.this,gyroscope,SensorManager.SENSOR_DELAY_NORMAL);
click.setText("Stop");
decide=0;
}else if(decide==0){
sm.unregisterListener(Accelerometer.this);
click.setText("Start");
decide=1;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
txt.setText(event.values[0]+"::"+event.values[1]+"::"+event.values[2]);
}
}
代码不起作用。任何示例,教程也会有所帮助。提前致谢。