广播接收器仅在 android 饼图中关闭应用程序时不工作

Broadcast receiver is not working only when app is closed in android pie

我每次都使用 Broadcast Receiver 来触发 incoming messages。它在 Android O 中工作正常,无论应用程序是否关闭。但在 Android P 中,它仅在应用程序运行时有效,而在应用程序关闭时无效。在 Android P 中,无论应用程序关闭与否,它都应该始终有效。我遵循了这个 link 和许多其他方法,但问题仍然存在。

清单中的接收者注册

<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>

广播接收器Class

    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));
    }
}

还有什么我遗漏的吗?

花了几天时间,我发现了真正的问题。

我的代码在 Android OAndroid P[上工作正常 =28=] 在 foregroundbackground 中,但是当我从最近的应用程序列表中清除该应用程序时,它在某些设备上停止工作,因为

厂商默认添加任务管理器功能,强制停止应用程序进行memory/battery管理。但很少有像 Whatsapp、Facebook 这样的应用程序有效。这可能是因为他们会将最著名的应用程序列入白名单。

更多信息请关注下方link

From 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) {

    }
}