Unregistering SensorManager doesn't work

前端 未结 3 1041
误落风尘
误落风尘 2021-01-15 04:42

In my app, I am using the light and proximity sensor to detect phone out of pocket functionality and then unregistering the SensorManager when their detection is complete. B

相关标签:
3条回答
  • 2021-01-15 04:55

    I was able to resolve this by correctly matching when the listeners were registered and when they were unregistered. Perhaps, initially the listeners weren't getting properly unregistered in all Call conditions (incoming call, outgoing call, missed call etc), so even when the activity closed, the listeners were still listening for events thus unnecessarily consuming power.

    0 讨论(0)
  • 2021-01-15 05:11
    mSensorManager.registerListener(YourListener.this, mSensorManager
                        .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                        SensorManager.SENSOR_DELAY_NORMAL);
    

    take this to register your Listener... then works your unregisterListener

    0 讨论(0)
  • 2021-01-15 05:13

    Just put this register code into main handler, then it works. but I don't know why.

    Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mSensorManager != null) {
                    mSensorManager.registerListener(sensorEventListener, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题