我需要在我的應用程序中計算yz角度。我正在嘗試使用陀螺儀,但它不起作用。我的手機甚至沒有進入功能onSensorChanged。你可以幫我嗎?這是我的代碼如何在Android中使用陀螺儀
MainActivity:
public class mainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private float xy_angle;
private float xz_angle;
private float yz_angle;
private TextView yzPlane;
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
protected void onResume()
{
super.onResume();
}
protected void onPause()
{
super.onPause();
}
public void onSensorChanged(SensorEvent sensorEvent)
{
xy_angle = sensorEvent.values[0];
xz_angle = sensorEvent.values[1];
yz_angle = sensorEvent.values[2];
Log.i("Test", "Trying to change sensor" + String.valueOf(xy_angle));
yzPlane.setText("Angle is: " + String.valueOf(yz_angle));
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
yzPlane = (TextView) findViewById(R.id.yzPlane);
}
我也有用途,功能清單文件:
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true"/>
我有索尼XPERIA M2所以它有一個陀螺儀。但亞行給我這個錯誤:
21681-21681/com.example.orientationsensor E/SensorManager﹕ sensor or listener is null
'我有索尼Xperia M2,所以它有一個陀螺儀。[引用需要] – njzk2