zoukankan      html  css  js  c++  java
  • 跨应用发送和接收广播

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

    发送广播的应用中:

    Java代码   收藏代码
    1. Intent intent = new Intent("info.zhegui.receiver.interprocess");  
    2. sendBroadcast(intent);  

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

    Java代码   收藏代码
    1. <uses-permission android:name="info.zhegui.receiver.RECEIVE"/>  

    接收广播的应用中:

    Java代码   收藏代码
    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的几个属性

    Java代码   收藏代码
    1. <permission android:name="info.zhegui.receiver.RECEIVE" />  
    2.   
    3. <application  
    4.     android:allowBackup="true"  
    5.     android:icon="@drawable/ic_launcher"  
    6.     android:label="@string/app_name"  
    7.     android:theme="@style/AppTheme" >  
    8.     <receiver  
    9.         android:name="info.zhegui.receiver.MyReceiver"  
    10.         android:exported="true"  
    11.         android:permission="info.zhegui.receiver.RECEIVE"  
    12.         android:process=":remote" >  
    13.         <intent-filter>  
    14.             <action android:name="info.zhegui.receiver.interprocess" />  
    15.         </intent-filter>  
    16.     </receiver>  
    17. </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

  • 相关阅读:
    青少年机器人技术等级考试实际操作试卷(三级)201812 new
    SQL预编译 new
    青少年机器人技术等级考试实际操作试卷(三级)201809 new
    青少年机器人技术等级考试实际操作试卷(三级)201803 new
    SQL基本练习 new
    Asp.Net MVC 自定义一个ActionResult用于AJAX交互
    使用 TeamLab 来协同和管理工作
    使用Chose来美化Select
    在Asp.Net下使用couchbase实现分布式缓存
    如何修改couchbase的RAM
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3225866.html
Copyright © 2011-2022 走看看