• Broadcase Receiver 广播接收器详解


     

    Android中Broadcast Receiver组件详解

    BroadcastReceiver(广播接收器)是Android中的四大组件之一。

     

    下面是Android Doc中关于BroadcastReceiver的概述:
    ①广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播──比如说,通知其它应用程序一些数据下载完成并处于可用状态。
    ②应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自BroadcastReceiver基类。
    ③广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。

     

    Android中的广播事件有两种,一种就是系统广播事件,比如:ACTION_BOOT_COMPLETED(系统启动完成后触发),ACTION_TIME_CHANGED(系统时间改变时触发),ACTION_BATTERY_LOW(电量低时触发)等等。另外一种是我们自定义的广播事件。

     

    广播事件的流程
    ①注册广播事件:注册方式有两种,一种是静态注册,就是在AndroidManifest.xml文件中定义,注册的广播接收器必须要继承BroadcastReceiver;另一种是动态注册,是在程序中使用Context.registerReceiver注册,注册的广播接收器相当于一个匿名类。两种方式都需要IntentFIlter
    ②发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action
    ③接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog

     

    下面我通过代码演示自定义广播事件和系统广播事件的使用。完整代码下载地址:android_broadcastreceiver.rar

    Step1:在MainActivityonStart方法中注册广播事件。静态注册方式是在AndroidManifest.xml文件中。

    Step2 点击相应按钮后会触发相应的方式来发送广播消息。

    [java] view plaincopy

    1. /** 
    2. * MainActivity 
    3. * @author zuolongsnail 
    4. */  
    5. public class MainActivity extends Activity {  
    6. private Button sendStaticBtn;  
    7. private Button sendDynamicBtn;  
    8. private Button sendSystemBtn;  
    9. private static final String STATICACTION = "com.byread.static";  
    10. private static final String DYNAMICACTION = "com.byread.dynamic";  
    11. // USB设备连接  
    12. private static final String SYSTEMACTION = Intent.ACTION_POWER_CONNECTED;  
    13. @Override  
    14. public void onCreate(Bundle savedInstanceState) {  
    15. super.onCreate(savedInstanceState);  
    16. setContentView(R.layout.main);  
    17. sendStaticBtn = (Button) findViewById(R.id.send_static);  
    18. sendDynamicBtn = (Button) findViewById(R.id.send_dynamic);  
    19. sendSystemBtn = (Button) findViewById(R.id.send_system);  
    20. sendStaticBtn.setOnClickListener(new MyOnClickListener());  
    21. sendDynamicBtn.setOnClickListener(new MyOnClickListener());  
    22. sendSystemBtn.setOnClickListener(new MyOnClickListener());  
    23. }  
    24. class MyOnClickListener implements OnClickListener{  
    25. @Override  
    26. public void onClick(View v) {  
    27. // 发送自定义静态注册广播消息  
    28. if(v.getId() == R.id.send_static){  
    29. Log.e("MainActivity", "发送自定义静态注册广播消息");  
    30. Intent intent = new Intent();  
    31. intent.setAction(STATICACTION);  
    32. intent.putExtra("msg", "接收静态注册广播成功!");  
    33. sendBroadcast(intent);  
    34. }  
    35. // 发送自定义动态注册广播消息  
    36. else if(v.getId() == R.id.send_dynamic){  
    37. Log.e("MainActivity", "发送自定义动态注册广播消息");  
    38. Intent intent = new Intent();  
    39. intent.setAction(DYNAMICACTION);  
    40. intent.putExtra("msg", "接收动态注册广播成功!");  
    41. sendBroadcast(intent);  
    42. }  
    43. // 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。  
    44. else if(v.getId() == R.id.send_system){  
    45. Log.e("MainActivity", "发送系统动态注册广播消息");  
    46. Intent intent = new Intent();  
    47. intent.setAction(SYSTEMACTION);  
    48. intent.putExtra("msg", "正在充电。。。。");  
    49. }  
    50. }  
    51. }  
    52. @Override  
    53. protected void onStart() {  
    54. super.onStart();  
    55. Log.e("MainActivity", "注册广播事件");  
    56. // 注册自定义动态广播消息  
    57. IntentFilter filter_dynamic = new IntentFilter();  
    58. filter_dynamic.addAction(DYNAMICACTION);  
    59. registerReceiver(dynamicReceiver, filter_dynamic);  
    60. // 注册系统动态广播消息  
    61. IntentFilter filter_system = new IntentFilter();  
    62. filter_system.addAction(SYSTEMACTION);  
    63. registerReceiver(systemReceiver, filter_system);  
    64. }  
    65. private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {  
    66. @Override  
    67. public void onReceive(Context context, Intent intent) {  
    68. Log.e("MainActivity", "接收自定义动态注册广播消息");  
    69. if(intent.getAction().equals(DYNAMICACTION)){  
    70. String msg = intent.getStringExtra("msg");  
    71. Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
    72. }  
    73. }  
    74. };  
    75. private BroadcastReceiver systemReceiver = new BroadcastReceiver() {  
    76. @Override  
    77. public void onReceive(Context context, Intent intent) {  
    78. Log.e("MainActivity", "接收系统动态注册广播消息");  
    79. if(intent.getAction().equals(SYSTEMACTION)){  
    80. String msg = intent.getStringExtra("msg");  
    81. Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
    82. }  
    83. }  
    84. };  
    85. }  

    Step3:接收广播消息。以下为两个静态注册的广播接收器。

    [java] view plaincopy

    1. /** 
    2. 自定义静态注册广播消息接收器 
    3. * @author zuolongsnail 
    4. */  
    5. public class StaticReceiver extends BroadcastReceiver {  
    6. @Override  
    7. public void onReceive(Context context, Intent intent) {  
    8. String msg = intent.getStringExtra("msg");  
    9. Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
    10. }  
    11. }  

    [java] view plaincopy

    1. /** 
    2. 系统静态注册广播消息接收器 
    3. *  
    4. * @author zuolongsnail 
    5. *  
    6. */  
    7. public class SystemReceiver extends BroadcastReceiver {  
    8. @Override  
    9. public void onReceive(Context context, Intent intent) {  
    10. if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {  
    11. Log.e("SystemReceiver", "电量低提示");  
    12. Toast.makeText(context, "您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show();  
    13. }  
    14. }  
    15. }  

    下面是AndroidManifest.xml文件:

    [xhtml] view plaincopy

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3. package="com.byread" android:versionCode="1" android:versionName="1.0">  
    4. <application android:icon="@drawable/icon" android:label="@string/app_name">  
    5. <activity android:name=".MainActivity" android:label="@string/app_name">  
    6. <intent-filter>  
    7. <action android:name="android.intent.action.MAIN" />  
    8. <category android:name="android.intent.category.LAUNCHER" />  
    9. </intent-filter>  
    10. </activity>  
    11. <!-- 注册自定义静态广播接收器 -->  
    12. <receiver android:name=".StaticReceiver">  
    13. <intent-filter>  
    14. <action android:name="com.byread.static" />  
    15. </intent-filter>  
    16. </receiver>  
    17. <!-- 注册系统静态广播接收器 -->  
    18. <receiver android:name=".SystemReceiver">  
    19. <intent-filter>  
    20. <action android:name="android.intent.action.BATTERY_LOW" />  
    21. </intent-filter>  
    22. </receiver>  
    23. </application>  
    24. </manifest>  

    界面布局文件main.xml

    [xhtml] view plaincopy

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3. android:orientation="vertical" android:layout_width="fill_parent"  
    4. android:layout_height="fill_parent">  
    5. <TextView android:layout_width="fill_parent"  
    6. android:layout_height="wrap_content" android:text="@string/hello" />  
    7. <Button android:id="@+id/send_static" android:layout_width="wrap_content"  
    8. android:layout_height="wrap_content" android:text="发送自定义静态注册广播" />  
    9. <Button android:id="@+id/send_dynamic" android:layout_width="wrap_content"  
    10. android:layout_height="wrap_content" android:text="发送自定义动态注册广播" />  
    11. <Button android:id="@+id/send_system" android:layout_width="wrap_content"  
    12. android:layout_height="wrap_content" android:text="发送系统动态注册广播" />  
    13. </LinearLayout>  

    讲解结束,不过有一点我自己也没弄清楚,这个系统广播事件如果我在程序中sendBroadcast的话,那就是自定义广播了。如果不写的话,那是不是系统自己来发送对应Action广播呢?有知道的同学请告诉我一下,再此先谢过。  

     

    运行界面:

     

     

     

  • 相关阅读:
    node相关--socket.io
    node相关--WebSocket
    node工具--express
    node工具--connect
    HTTP基础01--web与互联网基础
    nodeAPI--HTTP
    nodeAPI--TCP
    js:语言精髓笔记13--语言技巧
    js:语言精髓笔记12--动态语言特性(2)
    js:语言精髓笔记11--动态语言特性(1)
  • 原文地址:https://www.cnblogs.com/kuaileyuyi/p/3853197.html
Copyright © 2020-2023  润新知