Android SensorListener已弃用

时间:2013-05-24 14:19:08

标签: android android-sensors

我正在尝试更改以下代码以使用指南针,但是,我遇到了麻烦,我尝试使用SensorEventListener,但我失败了。我可以遵循任何教程吗?任何帮助将不胜感激。

private static SensorListener orientationListener;
    private static boolean isTrackingOrientation = false;

    @Override
    public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
        setContentView(R.layout.main);

        orientationListener = new SensorListener() {
            public void onSensorChanged(int s, float v[]) {
                float northDirection = v[android.hardware.SensorManager.DATA_X];
                ((QiblaCompassView)findViewById(R.id.qibla_compass)).setDirections(northDirection, VARIABLE.qiblaDirection);

            }
            public void onAccuracyChanged(int s, int a) {
            }
        }; 
    }

    @Override
    public void onResume() {
        startTrackingOrientation();
        super.onResume();
    }
    @Override
    public void onPause() {
        stopTrackingOrientation();
        super.onPause();
    }

    private void startTrackingOrientation() {
        if(!isTrackingOrientation) isTrackingOrientation = ((SensorManager)getSystemService(SENSOR_SERVICE)).registerListener(orientationListener, android.hardware.SensorManager.SENSOR_ORIENTATION);
    }
    private void stopTrackingOrientation() {
        if(isTrackingOrientation) ((SensorManager)getSystemService(SENSOR_SERVICE)).unregisterListener(orientationListener);
        isTrackingOrientation = false;
    }

1 个答案:

答案 0 :(得分:4)

这是传感器如何工作的一个很好的解释: http://developer.android.com/reference/android/hardware/SensorEvent.html

此代码成功运作:

package com.example.testvideo;

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;

public class MainActivity extends Activity implements SensorEventListener {

    SensorManager mSensorManager;
    Sensor accSensor;
    Sensor magnetSensor;

    float gravity[];
    float geoMagnetic[];
    float azimut;
    float pitch;
    float roll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this, accSensor);
        mSensorManager.unregisterListener(this, magnetSensor);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            gravity = event.values.clone();
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            geoMagnetic = event.values.clone();

        if (gravity != null && geoMagnetic != null) {

            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, gravity, geoMagnetic);
            if (success) {
                float orientation[] = new float[3];
                SensorManager.getOrientation(R, orientation);
                azimut = 57.29578F * orientation[0];
                pitch = 57.29578F * orientation[1];
                roll = 57.29578F * orientation[2];

                float dist = Math.abs((float) (1.4f * Math.tan(pitch * Math.PI / 180)));

                Log.d("log", "orientation values: " + azimut + " / " + pitch + " / " + roll + " dist = " + dist);
            }
        }
    }

}