标签:
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配置文件)
1 package com.qf.broadcastreceiver04; 2 3 public class Config { 4 5 //声明广播的Action 6 public static final String ACTION_PRINT="com.qf.broadcast.print"; 7 }
1 package com.qf.broadcastreceiver04; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 8 public class MainActivity extends Activity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 } 15 16 public void send1(View v){ 17 //发送打印广播: 有序广播,且带接收此广播的权限(需要在manifest文件中声明和使用) 18 //sendOrderedBroadcast(new Intent(Config.ACTION_PRINT),"com.qf.permission.print"); 19 20 //带有权限发送广播 21 sendBroadcast(new Intent(Config.ACTION_PRINT), "com.qf.permission.print"); 22 23 //sendBroadcast(new Intent(Config.ACTION_PRINT)); 24 } 25 26 }
1 package com.qf.broadcastreceiver04; 2 3 import java.util.Date; 4 5 import android.content.BroadcastReceiver; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.util.Log; 9 10 public class MyReceiver01 extends BroadcastReceiver { 11 12 @Override 13 public void onReceive(Context context, Intent intent) { 14 // TODO Auto-generated method stub 15 Log.i("debug", "--MyReceiver01---onReceive--"+new Date()); 16 } 17 18 }
4的另外两个和01一样
4的配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.qf.broadcastreceiver04" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="17" /> 10 11 <!-- 定义权限 --> 12 <permission android:name="com.qf.permission.print" /> 13 14 <!-- 使用权限 --> 15 <uses-permission android:name="com.qf.permission.print" /> 16 17 <application 18 android:allowBackup="true" 19 android:icon="@drawable/ic_launcher" 20 android:label="@string/app_name" 21 android:theme="@style/AppTheme" > 22 <activity 23 android:name="com.qf.broadcastreceiver04.MainActivity" 24 android:label="@string/app_name" > 25 <intent-filter> 26 <action android:name="android.intent.action.MAIN" /> 27 28 <category android:name="android.intent.category.LAUNCHER" /> 29 </intent-filter> 30 </activity> 31 32 <!-- 注册广播接收器 --> 33 <receiver 34 android:name="com.qf.broadcastreceiver04.MyReceiver01" 35 android:permission="com.qf.permission.print" > 36 <intent-filter> 37 <action android:name="com.qf.broadcast.print" /> 38 </intent-filter> 39 </receiver> 40 <receiver 41 android:name="com.qf.broadcastreceiver04.MyReceiver02" 42 android:permission="com.qf.permission.print" > 43 <intent-filter android:priority="80" > 44 <action android:name="com.qf.broadcast.print" /> 45 </intent-filter> 46 </receiver> 47 48 <!-- android:permission 设置接收的广播是带有权限的(发送广播端必须使用此权限) 49 android:exported="false" 不接收外部应用发送的广播 --> 50 <receiver 51 android:name="com.qf.broadcastreceiver04.MyReceiver03" 52 android:permission="com.qf.permission.print" 53 android:exported="false" > 54 55 <!-- 通过priority属性设置接收广播的优先级(范围: -1000~1000) --> 56 <intent-filter android:priority="100" > 57 <action android:name="com.qf.broadcast.print" /> 58 </intent-filter> 59 </receiver> 60 </application> 61 62 </manifest>
1 package com.qf.broadcastreceiver05; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 8 /** 9 * 接收04应用中发送的广播 10 * @author apple 11 * 12 */ 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 } 20 21 22 public void sendBroadcast(View v){ 23 Intent intent=new Intent("com.qf.broadcast.print"); 24 intent.setPackage("com.qf.broadcastreceiver04"); //设置可以接收此广播的应用的包名 25 26 sendBroadcast(intent); 27 } 28 29 }
5的recerver也是一样的
5的配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.qf.broadcastreceiver05" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="17" /> 10 11 <uses-permission android:name="com.qf.permission.print"/> 12 13 <application 14 android:allowBackup="true" 15 android:icon="@drawable/ic_launcher" 16 android:label="@string/app_name" 17 android:theme="@style/AppTheme" > 18 <activity 19 android:name="com.qf.broadcastreceiver05.MainActivity" 20 android:label="@string/app_name" > 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 24 <category android:name="android.intent.category.LAUNCHER" /> 25 </intent-filter> 26 </activity> 27 28 <receiver android:name="com.qf.broadcastreceiver05.MyReceiver01"> 29 <intent-filter> 30 <action android:name="com.qf.broadcast.print"/> 31 </intent-filter> 32 </receiver> 33 34 </application> 35 36 </manifest>
标签:
原文地址:http://www.cnblogs.com/bimingcong/p/4820533.html