android的广播broadcast和receiver
2014-02-07 14:02
197 查看
(1)基础概念
广播发送者:通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序。广播发送分两种:
A,同步广播:发送方发出后,几乎同时到达多个广播接收者处,并且无法终止广播继续传播,使用Context.sendBroadcast(intent);。
B,有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,而且能终止广播(abortBroadcast());使用Context.sendOrderedBroadcast(intent);
(2)发送接收模型核心代码
A,同步广播发送方:
Intent intent = new Intent();
intent.setAction("...");
Context.sendBroadcast(intent);
B,有序广播发送方:
Intent intent = new Intent();
intent.setAction("...");
Context.sendOrderedBroadcast(intent,null);
C,广播接收者核心代码:
public class Receiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
...
}
}
关于Intent filter的方式有两种:在注册receiver时用addAction,或者在androidmenifest.xml中加上。
(3) 普通广播实例,通过按钮发送一个广播,receiver中接收到后将广播中附带的信息在log中显示。
activity代码:
[java] view
plaincopy
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn =(Button)findViewById(R.id.callphone);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction("comzhang"); //intent filter的名字
intent.putExtra("name", "cheng"); //Intent中可以“键-值”的格式带数据
MainActivity.this.sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "send broadcast success", Toast.LENGTH_LONG).show(); //toast方式显示结果
}
});
}
receiver代码:
[java] view
plaincopy
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getExtras().getString("name"); //从intent中取出要带的参数,getString去处键对应的值
Log.i("zhangcheng","get :"+name);
}
}
andoid menifest的添加内容:
[html] view
plaincopy
<receiver android:name=".Receiver" //receiver文件名
android:exported="false">
<intent-filter>
<action android:name="comzhang"/> //匹配的Intent action名
</intent-filter>
eceiver>
执行它就可以在LOG中看到:get :cheng
(4)补充说明
A,在一个Service中发送广播的话,与在activity中有差别,需要在intent中加上标志setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。
B,以上实例是XML加Intent过滤广播的方式,也可以在程序中自定义广播的接收模式:
发送:
Intent startCameraIntent = new Intent("android.intent.action.HTC_START_CAMERA");
mContext.sendBroadcast(startCameraIntent);
接收:
ScreenFilter.addAction("android.intent.action.HTC_START_CAMERA") //字符串匹配就行
registerReceiver(mScreenFilterReceiver, ScreenFilter);
....................
if(intent.getAction().equals("android.intent.action.HTC_START_CAMERA")) {
}
(5) 关于有序广播的说明
有序广播可以设定接收的优先级,并在接收后删掉这个广播传送。在XML中设定receiver的优先级:
[html] view
plaincopy
<receiver android:name=".smsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
优先级别声明在intent-filter 元素的 android:priority 属性中,数越大优先级别越高,取值范围:-1000到1000,优先级别也可以调用IntentFilter对象的setPriority()进行设置。
有序广播的接收者可以终止广播Intent的传播,广播Intent的传播一旦终止,后面的接收者就无法接收到广播,使用abortBroadcast();即可。
参考原文:http://www.eoeandroid.com/thread-14095-1-1.html
参考原文:http://www.linuxidc.com/Linux/2012-07/65943.htm
参考原文:http://blog.csdn.net/prince58/article/details/6237792
广播发送者:通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序。广播发送分两种:
A,同步广播:发送方发出后,几乎同时到达多个广播接收者处,并且无法终止广播继续传播,使用Context.sendBroadcast(intent);。
B,有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,而且能终止广播(abortBroadcast());使用Context.sendOrderedBroadcast(intent);
(2)发送接收模型核心代码
A,同步广播发送方:
Intent intent = new Intent();
intent.setAction("...");
Context.sendBroadcast(intent);
B,有序广播发送方:
Intent intent = new Intent();
intent.setAction("...");
Context.sendOrderedBroadcast(intent,null);
C,广播接收者核心代码:
public class Receiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
...
}
}
关于Intent filter的方式有两种:在注册receiver时用addAction,或者在androidmenifest.xml中加上。
(3) 普通广播实例,通过按钮发送一个广播,receiver中接收到后将广播中附带的信息在log中显示。
activity代码:
[java] view
plaincopy
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn =(Button)findViewById(R.id.callphone);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction("comzhang"); //intent filter的名字
intent.putExtra("name", "cheng"); //Intent中可以“键-值”的格式带数据
MainActivity.this.sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "send broadcast success", Toast.LENGTH_LONG).show(); //toast方式显示结果
}
});
}
receiver代码:
[java] view
plaincopy
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getExtras().getString("name"); //从intent中取出要带的参数,getString去处键对应的值
Log.i("zhangcheng","get :"+name);
}
}
andoid menifest的添加内容:
[html] view
plaincopy
<receiver android:name=".Receiver" //receiver文件名
android:exported="false">
<intent-filter>
<action android:name="comzhang"/> //匹配的Intent action名
</intent-filter>
eceiver>
执行它就可以在LOG中看到:get :cheng
(4)补充说明
A,在一个Service中发送广播的话,与在activity中有差别,需要在intent中加上标志setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。
B,以上实例是XML加Intent过滤广播的方式,也可以在程序中自定义广播的接收模式:
发送:
Intent startCameraIntent = new Intent("android.intent.action.HTC_START_CAMERA");
mContext.sendBroadcast(startCameraIntent);
接收:
ScreenFilter.addAction("android.intent.action.HTC_START_CAMERA") //字符串匹配就行
registerReceiver(mScreenFilterReceiver, ScreenFilter);
....................
if(intent.getAction().equals("android.intent.action.HTC_START_CAMERA")) {
}
(5) 关于有序广播的说明
有序广播可以设定接收的优先级,并在接收后删掉这个广播传送。在XML中设定receiver的优先级:
[html] view
plaincopy
<receiver android:name=".smsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
优先级别声明在intent-filter 元素的 android:priority 属性中,数越大优先级别越高,取值范围:-1000到1000,优先级别也可以调用IntentFilter对象的setPriority()进行设置。
有序广播的接收者可以终止广播Intent的传播,广播Intent的传播一旦终止,后面的接收者就无法接收到广播,使用abortBroadcast();即可。
参考原文:http://www.eoeandroid.com/thread-14095-1-1.html
参考原文:http://www.linuxidc.com/Linux/2012-07/65943.htm
参考原文:http://blog.csdn.net/prince58/article/details/6237792
相关文章推荐
- android-BroadcastReceiver 发送一条普通广播
- BroadcastReceiver Android的广播解析
- 4.1.3.3 Android 网络状态监听的静态广播接受者和动态广播接受者Broadcast-Receiver
- Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
- Android 四大组件之 BroadcastReceiver 广播接收者
- Android广播接收机制(BroadcastReceiver)
- Android-广播BroadcastReceiver-自定义广播接受者和发送者
- 【BroadcastReceiver】广播接收器 broadcast sendOrderedBroadcast android
- android-BroadcastReceiver 发送有序广播
- Android的四大组件之一 BroadcastReceiver多种广播接收器的灵活使用
- Android中BroadcastReceiver接收系统广播
- Android之BroadcastReceiver 广播接受者(短信窃听实例)
- Android 广播BroadcastReceiver基础----重新认识Android(13)
- android广播接收者BroadcastReceiver
- android 广播机制Broadcast、BroadcastReceiver用法解读
- Android(java)学习笔记175:BroadcastReceiver之 外拨电话的广播接收者
- Android之BroadcastReceiver(二):接收系统广播
- Android(java)学习笔记178:BroadcastReceiver之 自定义广播
- android_BroadcastReceiver 广播机制
- Android学习Broadcast Receiver(广播接收者) 注意事项