Android学习笔记(十一)BroadcastReceiver动态注册、注销示例

  在上一篇博文中简单介绍了一下BroadcastReceiver的相关知识点,本篇举一个在代码中动态的注册、注销BroadcastReceiver的栗子。

1、首先创建一个MyReceiver并继承BroadcastReceiver,既然要动态注册该receiver的话,就无需在AndroidManifest.xml中配置它了:

 1 public class MyReceiver extends BroadcastReceiver {
 2     public static final String ACTION = "com.codingblock.receive.intent.action.MyReceiver";
 3 
 4     public MyReceiver() {
 5         
 6     }
 7 
 8     @Override
 9     public void onReceive(Context context, Intent intent) {
10         System.out.println("接收到的消息为:" + intent.getStringExtra("data"));
11     }
12 }

代码分析:上面代码中定义了一个静态变量ACTION,以方便程序指定该receiver。另外,从onReceive()方法的第二个参数intent可以获取到传递过来的数据。

2、布局文件如下,很简单,只有三个按钮:发送消息按钮、注册receiver按钮和注销receiver按钮。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_send_receiver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送消息" />
    
    <Button
        android:id="@+id/btn_register_receiver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册receiver" />
    
    <Button
        android:id="@+id/btn_unregister_receiver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注销receiver" />

</LinearLayout>

3、最后在MainActivity中测试一下

 1 public class MainActivity extends Activity implements OnClickListener {
 2     private MyReceiver receiver = null;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7         findViewById(R.id.btn_send_receiver).setOnClickListener(this);
 8         findViewById(R.id.btn_register_receiver).setOnClickListener(this);
 9         findViewById(R.id.btn_unregister_receiver).setOnClickListener(this);
10     }
11 
12     @Override
13     public void onClick(View v) {
14         switch (v.getId()) {
15         case R.id.btn_send_receiver:
16             Intent i = new Intent(MyReceiver.ACTION);
17             i.putExtra("data", "来自MainActivity的消息");
18             sendBroadcast(i);
19             break;
20         case R.id.btn_register_receiver:
21             if (receiver == null) {
22                 System.out.println("注册receiver");
23                 receiver = new MyReceiver();
24                 registerReceiver(receiver, new IntentFilter(MyReceiver.ACTION));
25             }
26             break;
27         case R.id.btn_unregister_receiver:
28             if (receiver != null) {
29                 System.out.println("注销receiver");
30                 unregisterReceiver(receiver);
31                 receiver = null;
32             }
33             break;
34 
35         default:
36             break;
37         }
38     }
39 }

代码分析:本段代码在23行,通过registerReceiver()方法动态的注册了MyReceiver,并且使用过后最好不要忘了用unregisterReceiver()方法再将其注销,注销后应将receiver设为null。

4、运行结果如下

Android学习笔记(十一)BroadcastReceiver动态注册、注销示例_第1张图片

测试就会发现在点击发送消息按钮之前,如果没有注册receiver的话是不能成功发送消息的,只有注册了receiver,点击发送消息log中才有输出:

从日志输出就能看出,当注册好了receiver后,按下发送消息按钮,在MyReceiver中就能收到来自MainActivity的消息。

 

你可能感兴趣的:(Android学习笔记(十一)BroadcastReceiver动态注册、注销示例)