使BroadcastReceiver在API 26或更高版本中在后台运行 [英] Making BroadcastReceiver work in the background in API 26 or above

查看:42
本文介绍了使BroadcastReceiver在API 26或更高版本中在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个有关我制作的应用程序以及我在清单中注册的BroadcastReceiver的问题,这是API级别26及更高版本所禁止的.我收到了许多有助于解决问题的好答案,其中之一是,我在活动本身中声明了BroadcastReceiver,并在其中以编程方式完成了意图过滤器和注册.

以下是我的问题的链接: 系统广播后没有出现对话框

我确实在之前制作的类似应用程序中做到了这一点,但我这样做的目的是在应用程序运行时不检测铃声模式更改,而在应用程序未运行时侦听它们,这意味着系统广播启动后,即会启动该应用的特定活动.因此,我想知道如何执行此操作,即,如果我无法在清单中注册接收器,则使BroadcastReceiver在运行API 26或更高版本的设备中接收系统广播.

P.S.我在运行Android API级别25的设备上测试了该应用程序,并且该应用程序可以正常运行,只是我在寻找针对新API的该技术的替代方法.

解决方案

但是在应用未运行时收听它们,这意味着应用的特定活动会在系统广播启动后立即开始

对于不在隐式广播白名单上的广播,您将需要有一个通过 registerReceiver()注册这些广播的前台服务.

I recently asked a question about an app that I made, and whose BroadcastReceiver I had registered in the Manifest, something that is prohibited in API level 26 and above. I received great answers to help resolve the problem, one of them being that I declare the BroadcastReceiver in the activity itself, with the intent filters and registering done right there programmatically.

Here is the link to my question: Dialog doesn't appear after system broadcast

I did do this in a similar app that I made earlier, but my purpose of making this is to not detect ringer mode changes when the app is running, but to listen to them when the app is not running, meaning that the specific activity of the app is started as soon as the system broadcast initiates. Therefore, I would like to know how to do this, i.e., make the BroadcastReceiver receive system broadcasts in devices running API 26 or more if I can't register the receiver in the manifest to do that.

P.S. I tested the app in a device running Android API level 25 and it worked perfectly as it should have, it's just that I am in the search for an alternative to this technique for newer APIs.

解决方案

but to listen to them when the app is not running, meaning that the specific activity of the app is started as soon as the system broadcast initiates

For broadcasts that are not on the implicit broadcast whitelist, you would need to have a foreground service that registers for those broadcasts via registerReceiver().

这篇关于使BroadcastReceiver在API 26或更高版本中在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆