Unregistering SensorManager doesn't work

社会主义新天地 提交于 2019-12-30 11:12:04

问题


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. But even when the CPU usage by the app shows just 1-2 sec usage, the battery usage always shows my app as no. 1 app in the list which is worrying.

I have used the SensorManager.unRegisterListener and also set SensorManager = null, but the situation remains the same.

I have read, that due to some bug, the sensors are not unregistered correctly. Any good way to dispose the sensors correctly ?

Pls guide. Omkar Ghaisas

Updated with Code sample from app -

@Override
protected void onPause()
{
    super.onPause();
    unHookReceiver();
}

private void unHookReceiver()
{
    if (r != null)
    {
        unregisterReceiver(r);
        if(GetProximityPreference("EnableReceiveByProximity"))
        {
            mySensorManager.unregisterListener(proximitySensorEventListener);
            mySensorManager.unregisterListener(lightSensorEventListener);
            mySensorManager = null;
            FileUtils.appendLog(FileUtils.GetCurrentDateTime() + " Power Consumption Log End");
            FileUtils.appendLog("------------------------------------------------");
        }
        r = null;
    }
}

I am also setting the sensorManager = null as per one suggestion from one post on stackpverflow, but even that doesn't help. In spite of calling the cleanup code, the battery usage is still very high. The app by itself should not be using much battery as its a very simple app with just one broadcast receiver and one activity, but within the activity, I invoke the Light and Proximity sensors and I doubt those are causing the spike in battery usage. Not sure why though.

Any help is highly appreciable.


回答1:


mSensorManager.registerListener(YourListener.this, mSensorManager
                    .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                    SensorManager.SENSOR_DELAY_NORMAL);

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




回答2:


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);
            }
        }
    });



回答3:


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.



来源:https://stackoverflow.com/questions/11071035/unregistering-sensormanager-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!