跨应用发送和接收广播
跨应用发送和接收广播,与同应用下的情况差不多,只需要添加一个权限,以及配置一下receiver的android:process属性即可
发送广播的应用中:
- Intent intent = new Intent("info.zhegui.receiver.interprocess");
- sendBroadcast(intent);
注意要在manifest.xml添加接收广播的权限,这个权限是receiver自定义的
- <uses-permission android:name="info.zhegui.receiver.RECEIVE"/>
接收广播的应用中:
- public class MyReceiver extends BroadcastReceiver {
- private final String TAG = this.getClass().getName();
- @Override
- public void onReceive(Context content, Intent intent) {
- Log.i(TAG, "intent:" + intent);
- }
- }
在manifest.xml中添加自定义权限,以及配置receiver的几个属性
- <permission android:name="info.zhegui.receiver.RECEIVE" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <receiver
- android:name="info.zhegui.receiver.MyReceiver"
- android:exported="true"
- android:permission="info.zhegui.receiver.RECEIVE"
- android:process=":remote" >
- <intent-filter>
- <action android:name="info.zhegui.receiver.interprocess" />
- </intent-filter>
- </receiver>
- </application>
需要注意的三个地方:
1,自定义权限
2,android:exported="true"
3,android:process=":remote" (有时候可以不要该属性)
参考文档:
http://developer.android.com/guide/topics/manifest/receiver-element.html
http://developer.android.com/training/articles/security-tips.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)