BroadcastReceiver with a Listener drains battery when not in use

前端 未结 1 1232
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 11:15

I have a receiver which waits for TelephonyManager.ACTION_PHONE_STATE_CHANGED:

public void onReceive(Context context, Intent intent) {
String th         


        
相关标签:
1条回答
  • 2021-01-16 11:55

    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.

    0 讨论(0)
提交回复
热议问题