注销的SensorManager不起作用不起作用、SensorManager

2023-09-04 13:15:52 作者:带着梦想出发

在我的应用程序,我现在用的是光线和距离传感器来检测手机自付功能,然后注销的SensorManager时,他们的检测完成。但是,即使当CPU使用率由应用程序显示只需1-2秒的使用,电池的使用总是显示我的应用程序,因为没有。 1应用程序中这是令人担忧的列表中。

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.

我已经使用了SensorManager.unRegisterListener,还可以设置的SensorManager = null,但情况依然一样。

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

我看过,这是由于一些bug,传感器是未注销正确。任何好的办法正确处理传感器?

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

请指导。 OMKAR Ghaisas

Pls guide. Omkar Ghaisas

更新与应用程序code样品 -

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

我也设置的SensorManager = null作为每一个建议,从一个职位stackpverflow,但即使这样做没有帮助。尽管调用清理code,电池的使用还是非常高的。本身的应用程序不应该用太多的电池作为其只有一个广播接收器和一个活动的一个非常简单的应用程序,但活动中,我调用亮度和接近传感器和我怀疑那些导致电池使用秒杀。不知道为什么,虽然。

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.

任何帮助是非常AP preciable。

Any help is highly appreciable.

推荐答案

我可以通过注册时所听众正确的匹配来解决这一点,当他们注销。也许,最初的听众都没有得到妥善未注册的所有呼叫条件(呼入,呼出,未接电话等),所以即使当活动结束时,听众仍然侦听事件从而不必要地消耗功率。

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.