加速度计编码无法在所有手机中使用

时间:2016-07-01 06:07:27

标签: android accelerometer

我正在开发Android音乐播放器,一旦检测到摇动动作,它将执行以下操作。动作:1。什么都不做2.播放或暂停3.下一首歌4.上一首歌

我为摇动动作创建了一个Service类,如下所示。

public class Shaker extends Service implements SensorEventListener {

    SharedPreferences sharedPreferences;
    private static final int MIN_TIME_BETWEEN_SHAKES_MILLISECS = 1000;
    private long mLastShakeTime;
    private SensorManager sensorManager;
    private static final String SHAKE_ACTION_PREFERENCE = "shake_action_preference";
    private static final String SHAKE_THRESHOLD_PREFERENCE = "shake_threshold_preference";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        super.onCreate();
    }

    public void onDestroy() {
        super.onDestroy();
        sensorManager.unregisterListener(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);

        return START_STICKY;
    }

    public void onStart(Intent intent, int startId) {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) {

                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];

                double acceleration = Math.sqrt(Math.pow(x, 2) +
                    Math.pow(y, 2) +
                    Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;
            Log.d("Acceleration", "Acceleration is " + acceleration + "m/s^2");

                String threshold = sharedPreferences.getString(SHAKE_THRESHOLD_PREFERENCE, "medium");

                float SHAKE_THRESHOLD;
                if(threshold.equals("high"))
                    SHAKE_THRESHOLD = 40.5f;
                else if(threshold.equals("low"))
                    SHAKE_THRESHOLD = 15.5f;
                else
                    SHAKE_THRESHOLD = 25.5f;
                if (acceleration > SHAKE_THRESHOLD) {
                    mLastShakeTime = curTime;
                    Log.d("Shake", "Shake, Rattle, and Roll");

                    String opt = sharedPreferences.getString(SHAKE_ACTION_PREFERENCE, "nothing");
                    Log.d("SharedPreference", opt);
                    Log.d("SharedPreference", threshold);
                    if(opt.equals("play")) {
                        MusicPlayer.playOrPause();
                    } else if(opt.equals("next")) {
                        MusicPlayer.next();
                    } else if(opt.equals("prev")) {
                        MusicPlayer.previous(getApplicationContext(), false);
                    }
                }
            }
        }
    }
}

但是当我在实际设备中运行apk文件时,摇动功能仅适用于某些手机而不适用于所有手机。

我尝试过使用相同Android操作系统的不同手机,但是它在一个中工作而在另一个中没有工作。

我无法弄清楚为什么会这样。任何人都可以为此提出解决方案吗?

1 个答案:

答案 0 :(得分:0)

我有这个问题。对于任何更改都会调用onSensorChanged方法,但在某些设备中,会多次调用它。这会导致代码逻辑出现问题。 我通过添加私有静态布尔变量来解决这个问题。 (它导致此方法调用一次)。一旦执行此方法,此变量​​将变为true。

 @Override
    public void onSensorChanged(SensorEvent event) {
         if(!variable) {
           variable=true;
           ...
           ...
           variable=false;
             }
         }