在Android O中使用意图广播USER_PRESENT
问题描述:
由于O加强了后台服务,因此在定位26岁以上的用户时,如何接收USER_PRESENT的广播?
Since O is more enforcing background services, how can I receive a broadcast of USER_PRESENT when targeting 26+ ?
Logcat:
system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.USER_PRESENT flg=0x24200010 } to fr.cab13140.nullcontactdeleter/.BootThread
答
接收广播的唯一方法是运行一个进程,在该进程中,您已使用 registerReceiver()
注册接收器.如果您一直尝试收听此广播,则获得的最接近的服务将是前台粘性服务,以最大程度地延长服务运行的时间.请注意,用户可能不会对此感到满意.
The only way to receive that broadcast is by having a running process, where you have used registerReceiver()
to register your receiver. If you are trying to listen to this broadcast all of the time, the closest that you will get will be to have a foreground sticky service, to try to maximize the amount of time your service is running. Note that users may not appreciate this.