0

I am confused about the unregistration of sensor listener. Suppose I forget to unregister a listener. What happens after the application is destroyed?

Will the Android OS keep sending message to the application? But the application is destroyed, and therefore its process is terminated. Anyone can help answer this question? Thanks :)

andrew
  • 795
  • 2
  • 7
  • 16

4 Answers4

5

But the application is destroyed, and therefore its process is terminated.

This is not always the case. Under normal conditions, Android will keep your application process alive as long as possible. If you happen to have listeners that are still registered, it is likely that old copies of the objects referenced by these listeners (most likely a subclass of Activity) will not be garbage collected and continue to eat up valuable memory. Sensor listeners are especially bad since the framework also spend other considerable resources needed to continue feeding them.

To demonstrate this problem, you can simply print out some log messages in a sensor listener that you intentionally kept registered. You will see that the log messages will continue to be printed out even if you exit your app normally. You can then also run MAT to inspect the memory of your process and it will likely show that a copy of your Activity object is still lingering in memory. If you have launched your app multiple times, MAT will show you that there are multiple "zombie" copies of your Activity still present :)

Joe
  • 13,858
  • 2
  • 37
  • 49
1

developer.android.com clearly states to

Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off.

http://developer.android.com/reference/android/hardware/SensorManager.html

monish_sc
  • 146
  • 6
0

Yes, you should do that, since the data will still be send to your app and I believe it's always a good programming style to "close" (unregister) something you "opened" (registered). I've written some example methods that demonstrate how to do this:

(Note: This code would have to be placed in the class that implements SensorEventListener, OnTouchListener, OnKeyListener)

/**
     * <b><i>public void useAccelerometer(boolean use)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Set if you would like to enable the use of the accelerometer.
     * 
     * @param use 
     * <br>
     * True will enable the use of the accelerometer.
     * <br>
     * False will disable the use of the accelerometer.
     *  
     */

    public void useAccelerometer(boolean use) {
        if(use == true) {
            manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Accelerometer enabled");
            }
        }
        else {
            manager.unregisterListener(this, accelerometer);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Accelerometer disabled");
            }
        }
    }

    /**
     * <b><i>public void useTouchscreen(boolean use)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Set if you would like to enable the use of the touch screen.
     * 
     * @param use 
     * <br>
     * True will enable the use of the touch screen.
     * <br>
     * False will disable the use of the touch screen.
     *  
     */

    public void useTouchscreen(boolean use) {
        if(use == true) {
            view.setOnTouchListener(this);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Touchscreen enabled");
            }
        }
        else {
            view.setOnTouchListener(null);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Touchscreen disabled");
            }
        }
    }

    /**
     * <b><i>public void useKeyboard(boolean use)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Set if you would like to enable the use of the keyboard.
     * 
     * @param use 
     * <br>
     * True will enable the use of the keyboard.
     * <br>
     * False will disable the use of the keyboard.
     *  
     */

    public void useKeyboard(boolean use) {
        if(use == true) {
            view.setOnKeyListener(this);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Keyboard enabled");
            }
        }
        else {
            view.setOnKeyListener(null);
            if(this.logGameEngineInputLog == true) {
                gameEngineLog.d(classTAG, "Keyboard disabled");
            }
        }
    }
Luke Taylor
  • 8,865
  • 13
  • 39
  • 69
0

Even if app is destroyed the sensor will continue to work as its not been unregistered..In totality if you register a sensor then you must unregister it too..

Snehal Poyrekar
  • 735
  • 5
  • 16