sendBroadcast(new Intent(Config.ACTION_PRINT),”com.qf.permission.print”);先判断应用有没有对应的权限 再去判断有没有对应的action两者都对应了才能进行接收      一个应用声明了权限 另一个应用使用了该权限并且action(频道相同)则可以接收广播

应用4发广播应用4里边对应频道的接受者都可以接受   应用5使用了应用4声明的权限并且接受者的action(频道)一直因此也可以接受4的广播   5发广播4也能接受 除非自己设置不接受其他应用的广播(见4的配置文件)  5也可以指定接受者的包名固定发给某个应用(见5的MainActivity.java)

配置文件里可以设置接受者的优先级越高先接受(见4配置文件)

 package com.qf.broadcastreceiver04;

 public class Config {

     //声明广播的Action
public static final String ACTION_PRINT="com.qf.broadcast.print";
}

4_Config.java

 package com.qf.broadcastreceiver04;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void send1(View v){
//发送打印广播: 有序广播,且带接收此广播的权限(需要在manifest文件中声明和使用)
//sendOrderedBroadcast(new Intent(Config.ACTION_PRINT),"com.qf.permission.print"); //带有权限发送广播
sendBroadcast(new Intent(Config.ACTION_PRINT), "com.qf.permission.print"); //sendBroadcast(new Intent(Config.ACTION_PRINT));
} }

4_MainActivity.java

 package com.qf.broadcastreceiver04;

 import java.util.Date;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; public class MyReceiver01 extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("debug", "--MyReceiver01---onReceive--"+new Date());
} }

4_MyReceiver01.java

4的另外两个和01一样

4的配置文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.broadcastreceiver04"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <!-- 定义权限 -->
<permission android:name="com.qf.permission.print" /> <!-- 使用权限 -->
<uses-permission android:name="com.qf.permission.print" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.broadcastreceiver04.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- 注册广播接收器 -->
<receiver
android:name="com.qf.broadcastreceiver04.MyReceiver01"
android:permission="com.qf.permission.print" >
<intent-filter>
<action android:name="com.qf.broadcast.print" />
</intent-filter>
</receiver>
<receiver
android:name="com.qf.broadcastreceiver04.MyReceiver02"
android:permission="com.qf.permission.print" >
<intent-filter android:priority="80" >
<action android:name="com.qf.broadcast.print" />
</intent-filter>
</receiver> <!-- android:permission 设置接收的广播是带有权限的(发送广播端必须使用此权限)
android:exported="false" 不接收外部应用发送的广播 -->
<receiver
android:name="com.qf.broadcastreceiver04.MyReceiver03"
android:permission="com.qf.permission.print"
android:exported="false" > <!-- 通过priority属性设置接收广播的优先级(范围: -1000~1000) -->
<intent-filter android:priority="100" >
<action android:name="com.qf.broadcast.print" />
</intent-filter>
</receiver>
</application> </manifest>

AndroidManifest.xml

 package com.qf.broadcastreceiver05;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; /**
* 接收04应用中发送的广播
* @author apple
*
*/
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void sendBroadcast(View v){
Intent intent=new Intent("com.qf.broadcast.print");
intent.setPackage("com.qf.broadcastreceiver04"); //设置可以接收此广播的应用的包名 sendBroadcast(intent);
} }

5_MainActivity.java

5的recerver也是一样的

5的配置文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.broadcastreceiver05"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <uses-permission android:name="com.qf.permission.print"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.broadcastreceiver05.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <receiver android:name="com.qf.broadcastreceiver05.MyReceiver01">
<intent-filter>
<action android:name="com.qf.broadcast.print"/>
</intent-filter>
</receiver> </application> </manifest>

5_AndroidManifest.xml

两个应用之间传递广播的规则 Broadcast的更多相关文章

  1. 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制

    原文:http://blog.csdn.net/jason0539/article/details/18075293 这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见 ...

  2. spring boot 之如何在两个页面之间传递值(转)

    原文地址:spring boot 之如何在两个页面之间传递值 问题:页面之间的跳转,通常带有值的传输,但是,在现在比较流行的SPRING MVC WEB 开发模型中,设计机制导致页面之间的直接接跳转和 ...

  3. 用WM_COPYDATA消息来实现两个进程之间传递数据

    文着重讲述了如果用WM_COPYDATA消息来实现两个进程之间传递数据. 进程之间通讯的几种方法:在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯.常用的方法有   1.使用内存映射 ...

  4. 两个activity之间传递数据用startActivityForResult方法。

    package com.example.testactivityresquest; import android.app.Activity; import android.content.Intent ...

  5. 如何在两个activity之间传递bitmap

    1.需求 在项目开发过程中,打印小票前需要添加打印预览功能,交易数据在打印前转成bitmap然后直接打印,为了显示这个bitmap需要将其传给显示activity. 2.解决方法 把bitmap存储为 ...

  6. WinForm 中两个窗口之间传递数据

    方法有很多种,这里介绍项目中使用的两种 一.通过委托+事件的方法进行传值 (点击Form2中的button1按钮,将会把Form2中的textbox.text 传给Form1中的 lable.text ...

  7. golang 与 c语言 之间传递指针的规则提案

    https://go.googlesource.com/proposal/+/master/design/12416-cgo-pointers.md https://github.com/golang ...

  8. 两个Fragment之间传递数据

    1.第一个Fragment BlankFragment blankFragment = new BlankFragment();Bundle bundle = new Bundle();bundle. ...

  9. Linux 两台服务器之间传递文件

    参考: https://www.cnblogs.com/clovershell/p/9870603.html linux采用scp命令拷贝文件到本地,拷贝本地文件到远程服务器   // 假设远程服务器 ...

随机推荐

  1. Javascript框架

    网易开源框架http://www.oschina.net/p/nej http://www.linuxeden.com/html/develop/20120716/127404.html 16 款最流 ...

  2. eclipse 常用jar包总结

    BeanUtils: DbUtils: FileUpload: IO: Lang: Logging: cglib: mysql-connector: Pool:[datasource] DBCP:[d ...

  3. php中的释放语句unset和释放函数mysql_free_result()

    首先要强调的一点是unset在php中已经不再是一个函数了,既然不是函数,那么就没有了返回值,所以用的时候不能够用unset的返回值来做判断. 其次,在函数中,unset只能销毁局部变量,并不能销毁全 ...

  4. jmeter建立JDBC连接池时遇到“A Test is currently running,stop or shutdown test to execute this command”

    1.显示如下图,打开日志可以看到:Variable Name must not be empty for element:JDBC Connection Configuration,即JDBC Con ...

  5. python入门第0篇 Windows下python的安装及pip安装和使用

    知识内容: 1. python2和python3安装 2. pip安装及pip命令使用 注:安装python3就可以了,python2除非项目开发需要否则不用安装,目前学习python就使用pytho ...

  6. python 字符串与字节之间的相互转化

    1.将字符串转化成字节 b'fffff' bytes('ffff', encoding='utf-8') 'ffff'.encode('utf-8') 2.将字节转化成字符串 str(data, en ...

  7. python入门-异常

    1 报错的例子 print(5/0) 2跳过报错的例子 try: print(5/0) except ZeroDivisionError: print("You can't divide b ...

  8. Amazon AWS S3 操作手册

    Install the SDK The recommended way to use the AWS SDK for Java in your project is to consume it fro ...

  9. jenkins坑—— shell 命令返回空导致构建失败

    今天用jenkins做CI遇到个坑,命令为:isSnapshot=`ls|grep isv-osp-service|grep -i snapshot` ls命令返回空的话,Jenkins构建就直接失败 ...

  10. parseInt 和 parseFloat 实现字符串转换为数字

    age = '18' a = parseInt(age) b = parseFloat(age)