BroadcastReceiver with a Listener drains battery when not in use

◇◆丶佛笑我妖孽 提交于 2019-12-01 13:03:10

Where does this battery drain come from?

Never never never never never never never never have something in a manifest-registered BroadcastReceiver live beyond onReceive(). IOW, you cannot safely register a SensorListener from a manifest-registered BroadcastReceiver.

Please have your BroadcastReceiver delegate work to a Service (via startService()), where in onStartCommand() you register the SensorListener. When you get a sensor reading, or after a timeout (hint: phones don't always change orientation), or in onDestroy() (if Android decides to shut down your service), unregister the listener and call stopSelf() to shut down the service.

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