静态注册就是在AndroidManifest.xml文件中定义,注册的广播接收器必须继承BroadReceiver
动态注册就是在程序中使用Context.registerReceiver注册。
发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。
接收广播:当发送的广播被接收器监听到后,会调用onReceive()方法,并将包含消息的Intent对象传回。
使用案例:
1、结构图:
2、Sample2-1_Activity.java代码如下:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 | package
import
import
import
import
import
import
import
import
import
import
public Sample2_1_Activity extends
private
//发送自定义静态注册广播的按钮 private
//发送自定义动态注册广播的按钮 private final String STATICACTION = "com.bn.pp2.staticreceiver" ; //静态广播的Action字符串 private final String DYNAMICACTION = "com.bn.pp2.dynamicreceiver" ; //动态广播的Action字符串 @Override public onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); sendStaticButton = (Button) findViewById(R.id.send_static); //获取Button按钮引用 sendDynamicButton = (Button) findViewById(R.id.send_dynamic); sendStaticButton.setOnClickListener( new
//为Button按钮添加监听器 sendDynamicButton.setOnClickListener( new
} class
implements
//内部类OnClick监听器 public onClick(View v) { if (v.getId() == R.id.send_static){ // 发送自定义静态注册广播消息 Intent intent = new
intent.setAction(STATICACTION); //设置Action intent.putExtra( "msg" , "接收静态注册广播成功!" ); //添加附加信息 sendBroadcast(intent); //发送Intent } else
(v.getId() == R.id.send_dynamic){ // 发送自定义动态注册广播消息 Intent intent = new
intent.setAction(DYNAMICACTION); //设置Action intent.putExtra( "msg" , "接收动态注册广播成功!" ); //添加附加信息 sendBroadcast(intent); //发送Intent }}} @Override protected onStart() { super .onStart(); IntentFilter dynamic_filter = new
dynamic_filter.addAction(DYNAMICACTION); //添加动态广播的Action registerReceiver(dynamicReceiver, dynamic_filter); // 注册自定义动态广播消息 } private
//动态广播的Receiver = new
@Override public onReceive(Context context, Intent intent) { if (intent.getAction().equals(DYNAMICACTION)){ //动作检测 String msg = intent.getStringExtra( "msg" ); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); }}};} |
3、AndroidManifest.xml代码如下:
12345678910111213141516171819 | <? xml
= "1.0"
= "utf-8" ?> < manifest
= "http://schemas.android.com/apk/res/android" android:versionCode = "1" android:versionName = "1.0"
= "com.bn.ex2_1" > < application
= "@drawable/icon"
= "@string/app_name" > < activity
= ".Sample2_1_Activity"
= "@string/app_name" > < intent-filter > < action
= "android.intent.action.MAIN"
< category
= "android.intent.category.LAUNCHER"
</ intent-filter > </ activity > <!-- 注册自定义静态广播接收器 --> < receiver
= ".StaticReceiver" > < intent-filter > < action
= "com.bn.pp2.staticreceiver"
</ intent-filter > </ receiver > </ application > </ manifest > |
4、StaticReceiver.java代码如下:
1234567891011 | package
import
import
import
import
public StaticReceiver extends
@Override
public onReceive(Context context, Intent intent) { String msg = intent.getStringExtra( "msg" ); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); }} |
5、main.xml代码如下:
12345678910111213141516171819202122 | <? xml
= "1.0"
= "utf-8" ?> < LinearLayout
= "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:textSize = "24dip" android:gravity = "center" android:text = "BroadcastReceiver演示"
< Button android:id = "@+id/send_static" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "发送自定义静态注册广播"
< Button android:id = "@+id/send_dynamic" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "发送自定义动态注册广播"
</ LinearLayout > |