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

跨应用发送和接收广播,与同应用下的情况差不多,只需要添加一个权限,以及配置一下receiver的android:process属性即可

 

发送广播的应用中:

  1. Intent intent = new Intent("info.zhegui.receiver.interprocess");  
  2. sendBroadcast(intent);  

 注意要在manifest.xml添加接收广播的权限,这个权限是receiver自定义的

  1. <uses-permission android:name="info.zhegui.receiver.RECEIVE"/>  

接收广播的应用中:

  1. public class MyReceiver extends BroadcastReceiver {  
  2.     private final String TAG = this.getClass().getName();  
  3.   
  4.     @Override  
  5.     public void onReceive(Context content, Intent intent) {  
  6.         Log.i(TAG, "intent:" + intent);  
  7.     }  
  8.   
  9. }  

 在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代码   收藏代码
  1. <permission android:description="string resource"  
  2.             android:icon="drawable resource"  
  3.             android:label="string resource"  
  4.             android:name="string"  
  5.             android:permissionGroup="string"  
  6.             android:protectionLevel=["normal" | "dangerous" |   
  7.                                      "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代码   收藏代码
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.testbutton"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="15" />  
  9.     <!-- 声明权限 -->  
  10.     <permission android:name="com.example.testbutton.RECEIVE" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             launcheMode="singleTask"  
  19.             android:configChanges="locale|orientation|keyboardHidden"  
  20.             android:screenOrientation="portrait"  
  21.             android:theme="@style/android:style/Theme.NoTitleBar.Fullscreen" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.         <!-- 注册Broadcast Receiver,并指定了给当前Receiver发送消息方需要的权限 -->  
  29.         <receiver  
  30.             android:name="com.example.testbutton.TestButtonReceiver"  
  31.             android:permission="com.example.testbutton.RECEIVE" >  
  32.             <intent-filter>  
  33.                 <action android:name="com.test.action" />  
  34.             </intent-filter>  
  35.         </receiver>  
  36.     </application>  
  37.   
  38. </manifest>  


app B 的menifest 文件内容 
Xml代码   收藏代码
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.testsender"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="15" />  
  9.     <!-- 声明使用指定的权限 -->  
  10.     <uses-permission android:name="com.example.testbutton.RECEIVE" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             android:label="@string/title_activity_main" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  


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

另外,也可在app B 的menifest文件中声明权限时,添加android:protectionLevel=“signature”,指定app B只能接收到使用同一证书签名的app 发送的消息。 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值