安卓9.0系统中,应用关闭后广播接收器无法工作

10

我正在使用 Broadcast Receiver 每次触发来检测 来电短信。在 Android O 中无论应用程序是否关闭,它都能正常工作。但是在 Android P 中,只有当应用程序处于活动状态时,它才能正常工作,而当应用程序关闭时,它就无法正常工作。在 Android P 中,它应该始终在应用程序关闭或未关闭的情况下正常工作。我按照这个链接和其他许多链接进行了尝试,但问题仍然存在。

在清单中注册接收器

<receiver
            android:name=".Broadcast.SmsListener"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

广播接收器类

    public class SmsListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Resulted12", "Into onReceive()");
        context.startService(new Intent(context, BackgroundService.class));
    }
}

我有没有漏掉其他的事情?


  1. 我的应用程序不是默认设置。
  2. 我正在做一些事情,从中我可以确定BR是否触发。
  3. 是的,我在模拟器和真实设备上都进行了测试。
在Android O上它运行良好,但在Android P上不行。
- Adeel Iftikhar
在移除SMS_DELIVER <action>后,无论应用程序是否在运行状态下,当Android P接收到新消息时,BR都会触发。 - Adeel Iftikhar
我在提问。 - Adeel Iftikhar
你能告诉我问题出在哪里吗? 我应该做哪些更改? - Adeel Iftikhar
@Mike M,我编辑了问题。现在你可以通过触发BR来看到我想要做什么。 - Adeel Iftikhar
服务在Android O中运行良好。 - Adeel Iftikhar
2个回答

11

经过几天的调试,我找到了真正的问题所在。

我的代码在Android OAndroid P前台后台都可以正常工作,但当我从最近的应用列表中清除该应用时,它会在某些设备上停止工作,原因是:

制造商默认添加了任务管理器功能,以进行内存/电池管理来强制停止应用程序。但像WhatsApp、Facebook等少数应用程序可以正常工作。这可能是因为它们已将最著名的应用程序列入白名单。

有关更多信息,请访问以下链接

清除最近的应用程序清除了应用程序的内存并导致我的接收器停止工作


0

来自 Android O 制造商的设置电池优化可以防止广播接收器在应用程序从后台中删除时调用。因此,您可以放置弹出窗口,该窗口将重定向到应用程序电池优化设置,并且用户可以切换到不优化选项,如果他们想在后台运行应用程序。

在清单中添加权限: android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"

private fun checkAndAskForBatteryOptimization() {
  
try {


        if (Build.VERSION.SDK_INT >= 28) {

            val powerManager = this.getApplicationContext().getSystemService(POWER_SERVICE) as PowerManager
            if (!powerManager.isIgnoringBatteryOptimizations(getPackageName())) {


                val intent = Intent()
                intent.action = "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
                if (Build.MANUFACTURER.toUpperCase() == "VIVO") {
                    startActivityForResult(Intent("android.settings.SETTINGS"), 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase() == "OPPO" || Build.MANUFACTURER.toUpperCase() == "REALME") {
                    val intent3 = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
                    intent3.data = Uri.fromParts("package", packageName, null as String?)
                    startActivityForResult(intent3, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase() == "XIAOMI") {
                    intent.action = "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
                    intent.data = Uri.parse("package:" + packageName)
                    startActivityForResult(intent, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase().contains("ONEPLUS")) {
                    val intent4 = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
                    intent4.data = Uri.fromParts("package", packageName, null as String?)
                    startActivityForResult(intent4, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase().contains("SAMSUNG")) {

                } else {
                    intent.data = Uri.parse("package:" + packageName)
                    startActivityForResult(intent, 50)
                }
            } else {

            }
        } else {

        }
    } catch (e: java.lang.Exception) {

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接