ITeye博客

跨应用(跨进程)发送广播和接收广播

xinanadu · 2013-03-16 10:36 · android · 移动开发 · Android · Broadcast · receiver · Markdown

跨应用发送和接收广播,与同应用下的情况差不多,只需要添加一个权限,以及配置一下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: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

评论(5)

  1. #5 楼 · wsygyb · 2019-02-19 10:03

    收不到广播,您有测试过吗????

  2. #4 楼 · jun117117 · 2015-04-12 14:17

    收不到广播,有能收到广播的例子帮忙分享下吧:jun117117@163.com

  3. #3 楼 · u012398365 · 2014-10-13 15:10

    查了好多资料都是说这样实现,但就是收不到广播,楼主知道原因不?会不会是android版本问题?

  4. #2 楼 · vigiles · 2014-08-26 13:06

    sendOrderedBroadcast(i, null,其它应用接收对象, null, 0, "广播内容", null); 这个该如何实现?

  5. #1 楼 · woshichenjun · 2014-07-09 11:53

    收不到广播

评论功能维护中,当前仅展示历史评论。