简体   繁体   English

Oreo中的SMS_RECEIVED广播接收器和JobIntentService

[英]SMS_RECEIVED Broadcast Receiver and JobIntentService in Oreo

I have read the Android documentation regarding the new background restrictions in Android API Level 26, and with those in mind, I have designed my app as follows: 我已阅读有关Android API Level 26中新背景限制的Android文档,并牢记这些,我对应用的设计如下:

  • The app has a BroadcastReceiver listening for the "SMS_RECEIVED" broadcast. 该应用程序具有一个BroadcastReceiver用于监听"SMS_RECEIVED"广播。 This is an implicit broadcast, which are now restricted in Oreo, however it is one of those in their exceptions list . 这是一个隐式广播,现在在Oreo中受到限制,但是这是其例外列表中的广播之一。
  • When this BroadcastReceiver runs, it enqueues a new task to be performed by a JobIntentService . 当此BroadcastReceiver运行时,它使由JobIntentService执行的新任务JobIntentService When receiving the broadcast, the app should be temporarily whitelisted and therefore, the JobIntentService given a window of opportunity to do its thing. 收到广播时, 应将应用程序暂时列入白名单 ,因此JobIntentService提供了执行其操作的机会。

All of this seems to be working just fine, even if I close the app from the "recent apps" list. 即使我从“最近使用的应用程序”列表中关闭了该应用程序,所有这些似乎都可以正常工作。 However, I'm getting reports from some users that apparently every few days, they have to open the app again because it suddenly stops working in the background. 但是,我从一些用户那里收到报告,显然每隔几天,他们不得不再次打开该应用程序,因为该应用程序突然在后台停止运行。

Why does the OS suddenly stop sending the "SMS_RECEIVED" broadcast to my app? 为什么操作系统突然停止向我的应用发送"SMS_RECEIVED"广播? If it is not that, then it must be the JobIntentService not being allowed to run. 如果不是那样,则必须是不允许JobIntentService运行。 Why not? 为什么不? The app should be whitelisted immediately after receiving the SMS. 收到短信后,应立即将该应用列入白名单。 What am I misunderstanding? 我有什么误会?

Besides Doze, Android also contains functionality called App Standby . 除了Doze之外,Android还包含称为App Standby的功能。 As per the documentation: 根据文档:

App Standby defers background network activity for apps with which the user has not recently interacted. App Standby推迟了用户最近未与之交互的应用程序的后台网络活动。

The default 'recently interacted' time frame is 3 days, which corresponds with your reports of things stopping 'apparently every few days'. 默认的“最近互动”时间范围是3天,与您“每隔几天明显停止”的报告相对应。

Therefore what seems to be happening is your JobIntentService fires (your app is indeed still whitelisted after receiving the broadcast), but App Standby is preventing your JobIntentService from connecting to the network. 因此,似乎正在发生您的JobIntentService触发(您的应用在收到广播后确实仍被列入白名单),但是App Standby阻止了您的JobIntentService连接到网络。

You can confirm this is the case by following the Testing your app with App Standby instructions : 您可以按照“ 使用App Standby测试您的应用”说明来确认是否是这种情况:

adb shell dumpsys battery unplug
adb shell am set-inactive <packageName> true

You might consider looking through the Acceptable use cases for whitelisting to see if asking to ignore battery optimizations is an option for your use case. 您可以考虑浏览白名单可接受用例,以了解是否要求忽略电池优化是该用例的一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM