Java – sensoreventlistener in the service

I tried to implement sensoreventlistener, but for some reason nothing happened

public class Servicee extends Service {
private SensorManager sensorManager;
private long lastUpdate;
SensorEventListener listen;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
    // TODO Auto-generated method stub
    sensorManager = (SensorManager) getApplicationContext()
            .getSystemService(SENSOR_SERVICE);
    lastUpdate = System.currentTimeMillis();
    listen = new SensorListen();
    return START_STICKY;
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),"Started",1000).show();
    super.onCreate();
}

private void getAccelerometer(SensorEvent event) {
    float[] values = event.values;
    // Movement
    float x = values[0];
    float y = values[1];
    float z = values[2];

    float accelationSquareRoot = (x * x + y * y + z * z)
            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
    long actualTime = System.currentTimeMillis();
    if (accelationSquareRoot >= 7) //
    {
        if (actualTime - lastUpdate < 2000) {
            return;
        }
        lastUpdate = actualTime;
        Toast.makeText(this,"Device was shuffed _ " + accelationSquareRoot,Toast.LENGTH_SHORT).show();
        Vibrator v = (Vibrator) getApplicationContext().getSystemService(VIBRATOR_SERVICE);
        v.vibrate(1000);
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
    }
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    sensorManager.unregisterListener(listen);
    Toast.makeText(this,"Destroy",Toast.LENGTH_SHORT).show();
    super.onDestroy();
}
public class SensorListen implements SensorEventListener{

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            getAccelerometer(event);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor,int accuracy) {
        // TODO Auto-generated method stub

    }

}

What could be wrong?

Solution

I believe the problem is that, at least in the code provided, you have never registered to receive accelerometer events

You need a code to get the accelerometer sensor and register it; This should enter onstartcommand() before returning

Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(listen,accel,SensorManager.SENSOR_DELAY_NORMAL);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>