如何使用传感器管理器包?

时间:2014-03-05 17:54:31

标签: android

以下代码不适用于micromax或xolo智能手机。任何人都可以告诉我一个可以在任何智能手机上使用的解决方案吗?

public class mainmenu extends Activity implements SensorListener {
    private SensorManager sensorManager;
    private TextView outView;
    private int sensor = SensorManager.SENSOR_ORIENTATION;
    outView = (TextView) findViewById(R.id.output);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
protected void onResume() {
    super.onResume();
    Log.d(TAG, "onResume");
    sensorManager.registerListener(this, sensor);
}
protected void onPause() {
   super.onPause();
   Log.d(TAG, "onPause");
   sensorManager.unregisterListener(this, sensor);
}
public void onAccuracyChanged(int sensor, int accuracy) {
    Log.d(TAG, String.format("onAccuracyChanged sensor: %d accuraccy: %d",
    sensor, accuracy));
}
public void onSensorChanged(int sensorReporting, float[] values) {
    if (sensorReporting != sensor)
        return;
    float azimuth = Math.round(values[0]);
    float pitch = Math.round(values[1]);
    float roll = Math.round(values[2]);
    String out = String.format("X: %.2f\nY:%.2f\nZ %.2f", azimuth,
        pitch, roll);
    Log.d(TAG, out);
    outView.setText(out);
    if (roll < - 25)
    {
        String out1 = String.format("POTHOLE!!!!");
        Log.d(TAG, out1);
        outView.setText(out1);
    }
}

` 此代码适用于三星和索尼智能手机。

0 个答案:

没有答案