【问题标题】:Broadcast receiver is not working only when app is closed in android pie仅当应用程序在 android pie 中关闭时,广播接收器才起作用
【发布时间】:2021-06-25 21:09:35
【问题描述】:

我每次都使用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>

广播接收器类

    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 中却不行。
  • 移除 SMS_DELIVER 后,无论应用是否处于运行状态,BR 都会在新消息到达 Android P 时触发。
  • 我在问一个问题。
  • 您能说出问题所在吗?我应该做哪些改变?
  • @Mike M 我编辑了这个问题。现在你可以通过触发 BR 看到我想要做什么了。

标签: android background broadcastreceiver


【解决方案1】:

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

我的代码在 Android O 和 Android P 上运行良好,在 前台 和 后台 但是当我从最近的应用程序列表中清除该应用程序时,它会因为

而停止在某些设备上运行

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

更多信息请点击以下链接

clear Recent apps wipe the apps memory and my receiver stopped working

【讨论】:

    【解决方案2】:

    来自 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) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多