デバイスが回転している角度を表示し、結果を度またはラジアンで表示するオプションを提供するメニューもあるアプリを作成しようとしていますが、このコードを実行した後、レイアウトを示す画面のみが表示されますデバイスを回転させても何も起こらない
package com.example.directordetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_name, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.degree:
m = 0;
return true;
case R.id.radian:
m = 1;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
TextView tvX = (TextView) findViewById(R.id.x_axis);
TextView tvY = (TextView) findViewById(R.id.y_axis);
TextView tvZ = (TextView) findViewById(R.id.z_axis);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE)
deltaX = (float) 0.0;
if (deltaY < NOISE)
deltaY = (float) 0.0;
if (deltaZ < NOISE)
deltaZ = (float) 0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
if (m == 1) {
deltaX = (float) (deltaX * (3.18 / 180));
deltaX = (float) (deltaX * (3.18 / 180));
deltaX = (float) (deltaX * (3.18 / 180));
}
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
}
}
}