您的位置:首页 > 其它

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

2014-08-28 10:26 309 查看
跨应用发送和接收广播,与同应用下的情况差不多,只需要添加一个权限,以及配置一下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
 在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序,系统或者用户带来负面影响的操作。如果应用需要执行某些操作,就需要声明使用这个操作对应的权限。 (在manifest文件中 添加<uses-permission>标记) 

   android 系统提供了一系列这样的权限,具体可以查看android
权限,另外,android系统在新的版本中会增加一些permission,可以查看android
版本信息。 

   当然,app也可以自定义属于自己的permission 或属于开发者使用的同一个签名的permission。定义一个permission 就是在menifest文件中添加一个permission标签。 

Xml代码  


<permission android:description="string resource"  

            android:icon="drawable resource"  

            android:label="string resource"  

            android:name="string"  

            android:permissionGroup="string"  

            android:protectionLevel=["normal" | "dangerous" |   

                                     "signature" | "signatureOrSystem"] />  

android:description :对权限的描述,一般是两句话,第一句话描述这个权限所针对的操作,第二句话告诉用户授予app这个权限会带来的后果 
android:label: 对权限的一个简短描述 
android:name :权限的唯一标识,一般都是使用 报名加权限名 
android:permissionGroup: 权限所属权限组的名称 
android:protectionLevel: 权限的等级, 
normal 是最低的等级,声明次权限的app,系统会默认授予次权限,不会提示用户 
dangerous  权限对应的操作有安全风险,系统在安装声明此类权限的app时会提示用户 
signature  权限表明的操作只针对使用同一个证书签名的app开放 
signatureOrSystem  与signature类似,只是增加了rom中自带的app的声明 

android:name 属性是必须的,其他的可选,未写的系统会指定默认值 

下面通过指定一个BroadcastReceiver的权限来实验 
首先创建了两个app,app A ,app B ; 
app A中注册了一个BroadcastReceiver ,app B 发送消息 
app A的menifest文件: 

Xml代码  


<manifest xmlns:android="http://schemas.android.com/apk/res/android"  

    package="com.example.testbutton"  

    android:versionCode="1"  

    android:versionName="1.0" >  

  

    <uses-sdk  

        android:minSdkVersion="7"  

        android:targetSdkVersion="15" />  

    <!-- 声明权限 -->  

    <permission android:name="com.example.testbutton.RECEIVE" />  

  

    <application  

        android:icon="@drawable/ic_launcher"  

        android:label="@string/app_name"  

        android:theme="@style/AppTheme" >  

        <activity  

            android:name=".MainActivity"  

            launcheMode="singleTask"  

            android:configChanges="locale|orientation|keyboardHidden"  

            android:screenOrientation="portrait"  

            android:theme="@style/android:style/Theme.NoTitleBar.Fullscreen" >  

            <intent-filter>  

                <action android:name="android.intent.action.MAIN" />  

  

                <category android:name="android.intent.category.LAUNCHER" />  

            </intent-filter>  

        </activity>  

        <!-- 注册Broadcast Receiver,并指定了给当前Receiver发送消息方需要的权限 -->  

        <receiver  

            android:name="com.example.testbutton.TestButtonReceiver"  

            android:permission="com.example.testbutton.RECEIVE" >  

            <intent-filter>  

                <action android:name="com.test.action" />  

            </intent-filter>  

        </receiver>  

    </application>  

  

</manifest>  

app B 的menifest 文件内容 

Xml代码  


<manifest xmlns:android="http://schemas.android.com/apk/res/android"  

    package="com.example.testsender"  

    android:versionCode="1"  

    android:versionName="1.0" >  

  

    <uses-sdk  

        android:minSdkVersion="7"  

        android:targetSdkVersion="15" />  

    <!-- 声明使用指定的权限 -->  

    <uses-permission android:name="com.example.testbutton.RECEIVE" />  

  

    <application  

        android:icon="@drawable/ic_launcher"  

        android:label="@string/app_name"  

        android:theme="@style/AppTheme" >  

        <activity  

            android:name=".MainActivity"  

            android:label="@string/title_activity_main" >  

            <intent-filter>  

                <action android:name="android.intent.action.MAIN" />  

  

                <category android:name="android.intent.category.LAUNCHER" />  

            </intent-filter>  

        </activity>  

    </application>  

  

</manifest>  

这样app B 给app A 发送消息,A就可以收到了,若未在app B的menifest文件中声明使用相应的权限,app B发送的消息,A是收不到的。 

另外,也可在app B 的menifest文件中声明权限时,添加android:protectionLevel=“signature”,指定app B只能接收到使用同一证书签名的app 发送的消息。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: