简体   繁体   English

停止服务后如何从传感器取消注册侦听器?

[英]How to unregister a listener from a sensor after stopping the service?

I'm creating an app that starts a service when "Start" button pressed and stops it when "Stop" button is pressed.我正在创建一个应用程序,它在按下“开始”按钮时启动服务并在按下“停止”按钮时停止它。 in the service, I register a listener for sensor ACCELEROMETER so that I get the accelerometer values of x,y,z axes.. but when I stop my application and unregister the listener from the sensor, even then I get my accelerometer values.在该服务中,我为传感器 ACCELEROMETER 注册了一个侦听器,以便获得 x、y、z 轴的加速度计值。

Here's the code:这是代码:

// Service
public class Accel extends Service
{
    private static Context CONTEXT; 
    private static Sensor sensor;
    private static SensorManager sensorManager;
    private static boolean running = false;

    @Override
    public void onCreate()
    {
    }

    // code to execute when the service is shutting down
    @Override
    public void onDestroy()
    {
        if (isListening())
            stopListening();
    }

    // code to execute when the service is starting up 
    @Override
    public void onStart(Intent intent, int startid)
    {
        CONTEXT = this;
        startListening(this);
    }

    public static Context getContext()
    {
        return CONTEXT;
    }

    // Returns true if the manager is listening to orientation changes
    public static boolean isListening()
    {
        return running;
    }

    //Unregisters listeners
    public static void stopListening()
    {
        running = false;
        sensorManager.unregisterListener(sensorEventListener, sensor);
    }

    /**
     * Registers a listener and start listening
     * @param accelerometerListener
     *             callback for accelerometer events
     */
    public static void startListening(AccelerometerListener accelerometerListener)
    {
        sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if (sensors.size() > 0)
        {
            sensor = sensors.get(0);
            running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
            listener = accelerometerListener;
        }
    }

    /**
     * The listener that listen to events from the accelerometer listener
     */
    private static SensorEventListener sensorEventListener = 
    new SensorEventListener()
    {
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    public void onSensorChanged(SensorEvent event)
    {
                // the code to perform on sensor change
    }
};

} }

Can anyone please help me out??有人可以帮我吗??

onDestroy() may not be called by the OS on your app straight away. onDestroy()可能不会立即被您的应用程序上的操作系统调用。 Place your stopListening() call into onPause() , and startListening() in your onResume() functions.stopListening()调用放入onPause() ,并将startListening()放入onResume()函数中。 This way you are guaranteed to have the app register and unregister itself with the sensors.这样你就可以保证应用程序在传感器上注册和取消注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM