문제

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.

도움이 되었습니까?

해결책 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.

다른 팁

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

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

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);
            }
        }
    });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top