Android注册广播

一、系统广播随着Android版本的变化

Android 14

Android 14(API level 34)

While apps are in a cached state, broadcast delivery is optimized for system health. For example, less important system broadcasts such as ACTION_SCREEN_ON are deferred while the app is in a cached state. Once the app goes from the cached state into an active process lifecycle, the system delivers any deferred broadcasts.

Important broadcasts that are declared in the manifest temporarily remove apps from the cached state for delivery.

Android 9

从Android 9 (API level 28)开始, 通知 NETWORK_STATE_CHANGED_ACTION 不再接收用户位置或者个人身份数据。

另外,App安装在Android 9 或者更高版本的手机上,WIFI的系统广播不再包含SSID,BSSID等连接信息或扫描结果。获取这些信息,需要调用getConnectionInfo()方法。

Android 8.0

从Android 8.0 (API level 26)开始,系统对在manifest中声明receiver做了额外的限制:

如果targetSdkVersion大于Android 8.0及以上版本(targetSdkVersion>=26),对于大部分"隐性广播"(implicit broadcasts)我们都不能在manifest中声明<receiver>来注册广播接收器。我们仍然可以使用registerReceiver(BroadcastReceiver, IntentFilter)注册广播接收器。

Android 7.0

Android 7.0 (API level 24) 及以上版本,不会发送以下系统广播:

groovy 复制代码
ACTION_NEW_PICTURE
ACTION_NEW_VIDEO

同样,targetSdkVersion指定为Android 7.0及以上版本的(targetSdkVersion>=24),必须使用

registerReceiver(BroadcastReceiver, IntentFilter)注册CONNECTIVITY_ACTION 广播.在manifest中声明<receiver>不会生效。

二、接受广播

有两种接受广播方式

1、在manifest中声明receivers

在manifest中声明广播receiver,当广播发出后,系统会启动我们的App(即便我们的App已经不再运行)

注意:如果我们的App设置targetSdkVersion>=26,对于隐性广播(implicit broadcasts)我们不能在manifest中声明receiver。
作者编写:既然Google已经意识到了在manifest中注册广播接收器,会启动我们的App存在安全风险,从API Level 26开始,就基本弃用manifest中注册的方式。

groovy 复制代码
<!-- If this receiver listens for broadcasts sent from the system or from
     other apps, even other apps that you own, set android:exported to "true". -->
<receiver android:name=".MyBroadcastReceiver" android:exported="false">
    <intent-filter>
        <action android:name="APP_SPECIFIC_BROADCAST" />
    </intent-filter>
</receiver>
java 复制代码
public class MyBroadcastReceiver extends BroadcastReceiver {
        private static final String TAG = "MyBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) {
            StringBuilder sb = new StringBuilder();
            sb.append("Action: " + intent.getAction() + "\n");
            sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
            String log = sb.toString();
            Log.d(TAG, log);

            ActivityNameBinding binding =
                    ActivityNameBinding.inflate(layoutInflater);
            val view = binding.root;
            setContentView(view);

            Snackbar.make(view, log, Snackbar.LENGTH_LONG).show();
        }
    }

2、基于Context注册广播receivers

groovy 复制代码
ContextCompat.registerReceiver(context, br, filter, receiverFlags);

unregisterReceiver(android.content.BroadcastReceiver)
相关推荐
火柴就是我3 小时前
Flutter 项目本地 Maven 引入 AAR 找不到?一次 Gradle 路径问题排查
android
火柴就是我3 小时前
Android 打包报错 25.0.3
android·前端
码上有光4 小时前
Linux进程通信——共享内存、消息队列和信号量
android·linux·运维·共享内存·通信
Android打工仔8 小时前
Kotlin 协程源码解析:DispatchedContinuation 里的 Dispatcher 从哪里来?
android·kotlin
打工仔折腾 AI8 小时前
从BPE到SentencePiece:Transformer分词原理与Python实战对比
android·人工智能·python·深度学习·langchain·transformer·ai agent 实战
Dovis(誓平步青云)9 小时前
几个方案来回选不定?做一个随时切换的候选推荐页
android·java·服务器·开发语言·javascript·数据库·智能化
恋猫de小郭12 小时前
KMP 又改了编译流程, Separate Compilation 禁止了 `commonMain` 的依赖穿透
android·前端·flutter
执明wa13 小时前
Android RecyclerView 实战:点击频道栏切换对应内容
android·开发语言·windows·microsoft·android studio
新鲜势力呀13 小时前
PHP 定时数据同步实战:从第三方接口超时到异步同步 + 增量更新架构优化全过程
android
星间都市山脉15 小时前
RecoveryUI 圆角屏安全边距配置及代码调用链
android·java·linux·windows·ubuntu