静态注册就是在AndroidManifest.xml文件中定义,注册的广播接收器必须继承BroadReceiverjava
动态注册就是在程序中使用Context.registerReceiver注册。android
发送广播事件:经过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。app
接收广播:当发送的广播被接收器监听到后,会调用onReceive()方法,并将包含消息的Intent对象传回。ide
使用案例:code
一、结构图:xml
二、Sample2-1_Activity.java代码以下:对象
package com.bn.ex2_1; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class Sample2_1_Activity extends Activity { private Button sendStaticButton; //发送自定义静态注册广播的按钮 private Button sendDynamicButton; //发送自定义动态注册广播的按钮 private static final String STATICACTION = "com.bn.pp2.staticreceiver"; //静态广播的Action字符串 private static final String DYNAMICACTION = "com.bn.pp2.dynamicreceiver"; //动态广播的Action字符串 @Override public void 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 DIYOnClickListener()); //为Button按钮添加监听器 sendDynamicButton.setOnClickListener(new DIYOnClickListener()); } class DIYOnClickListener implements OnClickListener{ //内部类OnClick监听器 public void onClick(View v) { if(v.getId() == R.id.send_static){ // 发送自定义静态注册广播消息 Intent intent = new Intent(); intent.setAction(STATICACTION); //设置Action intent.putExtra("msg", "接收静态注册广播成功!"); //添加附加信息 sendBroadcast(intent); //发送Intent } else if(v.getId() == R.id.send_dynamic){ // 发送自定义动态注册广播消息 Intent intent = new Intent(); intent.setAction(DYNAMICACTION); //设置Action intent.putExtra("msg", "接收动态注册广播成功!"); //添加附加信息 sendBroadcast(intent); //发送Intent }}} @Override protected void onStart() { super.onStart(); IntentFilter dynamic_filter = new IntentFilter(); dynamic_filter.addAction(DYNAMICACTION); //添加动态广播的Action registerReceiver(dynamicReceiver, dynamic_filter); // 注册自定义动态广播消息 } private BroadcastReceiver dynamicReceiver //动态广播的Receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(DYNAMICACTION)){ //动做检测 String msg = intent.getStringExtra("msg"); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); }}};}
三、AndroidManifest.xml代码以下:继承
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.bn.ex2_1"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Sample2_1_Activity" 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=".StaticReceiver"> <intent-filter> <action android:name="com.bn.pp2.staticreceiver" /> </intent-filter> </receiver> </application> </manifest>
四、StaticReceiver.java代码以下:事件
package com.bn.ex2_1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class StaticReceiver extends BroadcastReceiver { @Override //静态广播接收器执行的方法 public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("msg"); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); }}
五、main.xml代码以下:ip
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="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>
六、运行截图: