ShakeListener在单独的类中实现

时间:2012-07-18 11:49:47

标签: java android shake

我正在尝试实现ShakeListener。所以我创建了一个名为ShakeListener.java的类:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public class ShakeListener implements SensorEventListener {

    private SensorManager sensorManager;
    private List<Sensor> sensors;
    private Sensor sensor;
    private long lastUpdate = -1;
    private long currentTime = -1;

    private float last_x, last_y, last_z;
    private float current_x, current_y, current_z, currenForce;
    private static final int FORCE_THRESHOLD = 900;
    private final int DATA_X = SensorManager.DATA_X;
    private final int DATA_Y = SensorManager.DATA_Y;
    private final int DATA_Z = SensorManager.DATA_Z;

    public ShakeListener(Activity parent) {
        SensorManager sensorService = (SensorManager) parent
                .getSystemService(Context.SENSOR_SERVICE);
        this.sensorManager = sensorManager;

        this.sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if (sensors.size() > 0) {
            sensor = sensors.get(0);
        }
    }

    public void start() {
        if (sensor != null) {
            sensorManager.registerListener(this, sensor,
                    SensorManager.SENSOR_DELAY_GAME);
        }
    }

    public void stop() {
        sensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor s, int valu) {

    }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER
                || event.values.length < 3)
            return;

        currentTime = System.currentTimeMillis();

        if ((currentTime - lastUpdate) > 100) {
            long diffTime = (currentTime - lastUpdate);
            lastUpdate = currentTime;

            current_x = event.values[DATA_X];
            current_y = event.values[DATA_Y];
            current_z = event.values[DATA_Z];

            currenForce = Math.abs(current_x + current_y + current_z - last_x
                    - last_y - last_z)
                    / diffTime * 10000;

            if (currenForce > FORCE_THRESHOLD) {

                Log.d("ShakeListener", "SHAKE DETECTED");

            }
            last_x = current_x;
            last_y = current_y;
            last_z = current_z;

        }
    }

}

现在,我想从我的主要活动中调用它(考虑实现一个等待摇动布尔值改变的While循环?)。所以在我的主要活动中,我使用:

    private ShakeListener myShakeListener;

然后

    myShakeListener = new ShakeListener(this);
    myShakeListener.start();

但是我得到了NPE:

        this.sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

我做错了什么?我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

看看sensorService。这是错误的。更改您的代码:

SensorManager sensorService = (SensorManager) parent
                .getSystemService(Context.SENSOR_SERVICE);
        this.sensorManager = sensorManager;

SensorManager sensorService = (SensorManager) parent
                .getSystemService(Context.SENSOR_SERVICE);
        this.sensorManager = sensorService ;

或更好的代码:

 this.sensorManager = (SensorManager) parent.getSystemService(Context.SENSOR_SERVICE);