0

我对传感器侦听器的注销感到困惑。假设我忘记注销一个监听器。应用程序销毁后会发生什么?

Android 操作系统会继续向应用程序发送消息吗?但是应用程序被破坏了,因此它的进程被终止了。任何人都可以帮助回答这个问题吗?谢谢 :)

4

4 回答 4

5

但是应用程序被破坏了,因此它的进程被终止了。

这并非总是如此。在正常情况下,Android会尽可能长时间地保持您的应用程序进程处于活动状态。如果您碰巧有仍在注册的侦听器,则这些侦听器(很可能是 Activity 的子类)引用的对象的旧副本很可能不会被垃圾收集并继续消耗宝贵的内存。传感器侦听器尤其糟糕,因为该框架还花费了其他大量资源来继续为它们提供数据。

为了演示这个问题,您可以简单地在您有意保持注册的传感器侦听器中打印出一些日志消息。即使您正常退出应用程序,您也会看到日志消息将继续打印出来。然后,您还可以运行 MAT 来检查进程的内存,它可能会显示您的 Activity 对象的副本仍在内存中徘徊。如果您多次启动您的应用程序,MAT 会显示您的 Activity 仍然存在多个“僵尸”副本 :)

于 2012-08-02T11:12:13.550 回答
1

developer.android.com 明确声明

始终确保禁用您不需要的传感器,尤其是在您的活动暂停时。否则,可能会在几个小时内耗尽电池电量。请注意,当屏幕关闭时,系统不会自动禁用传感器。

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

于 2012-08-02T09:00:34.347 回答
0

是的,您应该这样做,因为数据仍会发送到您的应用程序,而且我相信这对您来说始终是一种很好的编程"close" (unregister)风格"opened" (registered)。我编写了一些示例方法来演示如何执行此操作:

(注意:此代码必须放在实现的类中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");
            }
        }
    }
于 2012-08-02T06:33:08.960 回答
0

即使应用程序被破坏,传感器仍将继续工作,因为它没有被取消注册..总的来说,如果您注册了一个传感器,那么您也必须取消注册它..

于 2012-08-07T14:29:48.930 回答