Android提高第六篇之BroadcastReceiver

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

前面分别讨论了Activity和Service,这次就轮到BroastcastReceiver,Broastcast是应用程序间通信的手段。BroastcastReceiver也是跟Intent紧密相连的,动态/静态注册了BroastcastReceiver之后,使用sendBroadcast把Intent发送之后,系统会自动把符合条件的BroastcastReceiver启动,跟嵌入式系统的中断类似。

本文主要演示了如何静态/动态注册BroastcastReceiver,向系统索取电量信息,以及枚举信息的字段。本文运行截图如下:

上图是发送Intent至内部动态注册的BroadcastReceiver,接收到之后显示消息名称。动态注册BroadcastReceiver用到registerReceiver()。

上图是发送Intent至内部静态注册的BroadcastReceiver,接收到之后显示消息名称。静态注册比动态注册麻烦点,先新建一个类继承BroadcastReceiver,然后到AndroidManifest.xml 添加

view plain print ?
  1. <receiverandroid:name="clsReceiver2">
  2. <intent-filter>
  3. <action
  4. android:name="com.testBroadcastReceiver.Internal_2"/>
  5. intent-filter>
  6. receiver>

第一个name是类名,第二个是action的名称。

上图是枚举Intent消息的字段,这个功能比较适合懒人,把收到的Intent消息的字段全部分解了,再看看哪个需要的,懒得记住。实现这部分的代码如下:

view plain print ?
  1. //当未知Intent包含的内容,则需要通过以下方法来列举
  2. Bundleb=intent.getExtras();
  3. Object[]lstName=b.keySet().toArray();
  4. for(inti=0;i
  5. {
  6. StringkeyName=lstName[i].toString();
  7. Log.e(keyName,String.valueOf(b.get(keyName)));
  8. }

main.xml的代码如下:

view plain print ?
  1. xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"android:text="发送至内部动态注册的BroadcastReceiver">Button>
  7. <Buttonandroid:id="@+id/Button02"android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"android:text="发送至内部静态注册BroadcastReceiver">Button>
  9. <Buttonandroid:id="@+id/Button03"android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"android:text="发送至系统BroadcastReceiver">Button>
  11. LinearLayout>

testBroadcastReceiver.java的代码如下:

view plain print ?
  1. packagecom.testBroadcastReceiver;
  2. importandroid.app.Activity;
  3. importandroid.content.BroadcastReceiver;
  4. importandroid.content.Context;
  5. importandroid.content.Intent;
  6. importandroid.content.IntentFilter;
  7. importandroid.os.Bundle;
  8. importandroid.util.Log;
  9. importandroid.view.View;
  10. importandroid.widget.Button;
  11. importandroid.widget.Toast;
  12. publicclasstestBroadcastReceiverextendsActivity{
  13. ButtonbtnInternal1,btnInternal2,btnSystem;
  14. staticfinalStringINTENAL_ACTION_1="com.testBroadcastReceiver.Internal_1";
  15. staticfinalStringINTENAL_ACTION_2="com.testBroadcastReceiver.Internal_2";
  16. staticfinalStringINTENAL_ACTION_3="com.testBroadcastReceiver.Internal_3";
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. btnInternal1=(Button)this.findViewById(R.id.Button01);
  22. btnInternal1.setOnClickListener(newClickEvent());
  23. btnInternal2=(Button)this.findViewById(R.id.Button02);
  24. btnInternal2.setOnClickListener(newClickEvent());
  25. btnSystem=(Button)this.findViewById(R.id.Button03);
  26. btnSystem.setOnClickListener(newClickEvent());
  27. //动态注册广播消息
  28. registerReceiver(bcrIntenal1,newIntentFilter(INTENAL_ACTION_1));
  29. }
  30. classClickEventimplementsView.OnClickListener{
  31. @Override
  32. publicvoidonClick(Viewv){
  33. if(v==btnInternal1)//给动态注册的BroadcastReceiver发送数据
  34. {
  35. Intentintent=newIntent(INTENAL_ACTION_1);
  36. sendBroadcast(intent);
  37. }
  38. elseif(v==btnInternal2)//给静态注册的BroadcastReceiver发送数据
  39. {
  40. Intentintent=newIntent(INTENAL_ACTION_2);
  41. sendBroadcast(intent);
  42. }
  43. elseif(v==btnSystem)//动态注册接收2组信息的BroadcastReceiver
  44. {
  45. IntentFilterfilter=newIntentFilter();//
  46. filter.addAction(Intent.ACTION_BATTERY_CHANGED);//系统电量检测信息
  47. filter.addAction(INTENAL_ACTION_3);//第三组自定义消息
  48. registerReceiver(batInfoReceiver,filter);
  49. Intentintent=newIntent(INTENAL_ACTION_3);
  50. intent.putExtra("Name","hellogv");
  51. intent.putExtra("Blog","http://blog.csdn.net/hellogv");
  52. sendBroadcast(intent);//传递过去
  53. }
  54. }
  55. }
  56. /*
  57. *接收动态注册广播的BroadcastReceiver
  58. */
  59. privateBroadcastReceiverbcrIntenal1=newBroadcastReceiver(){
  60. publicvoidonReceive(Contextcontext,Intentintent){
  61. Stringaction=intent.getAction();
  62. Toast.makeText(context,"动态:"+action,1000).show();
  63. }
  64. };
  65. privateBroadcastReceiverbatInfoReceiver=newBroadcastReceiver(){
  66. publicvoidonReceive(Contextcontext,Intentintent){
  67. Stringaction=intent.getAction();
  68. //如果捕捉到的action是ACTION_BATTERY_CHANGED
  69. if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
  70. //当未知Intent包含的内容,则需要通过以下方法来列举
  71. Bundleb=intent.getExtras();
  72. Object[]lstName=b.keySet().toArray();
  73. for(inti=0;i
  74. {
  75. StringkeyName=lstName[i].toString();
  76. Log.e(keyName,String.valueOf(b.get(keyName)));
  77. }
  78. }
  79. //如果捕捉到的action是INTENAL_ACTION_3
  80. if(INTENAL_ACTION_3.equals(action)){
  81. //当未知Intent包含的内容,则需要通过以下方法来列举
  82. Bundleb=intent.getExtras();
  83. Object[]lstName=b.keySet().toArray();
  84. for(inti=0;i
  85. {
  86. StringkeyName=lstName[i].toString();
  87. Log.e(keyName,b.getString(keyName));
  88. }
  89. }
  90. }
  91. };
  92. }

clsReceiver2.java的代码如下:

view plain print ?
  1. packagecom.testBroadcastReceiver;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.widget.Toast;
  6. /*
  7. *接收静态注册广播的BroadcastReceiver,
  8. *step1:要到AndroidManifest.xml这里注册消息
  9. *
  10. android:name="com.testBroadcastReceiver.Internal_2"/>
  11. step2:定义消息的字符串
  12. step3:通过Intent传递消息来驱使BroadcastReceiver触发
  13. */
  14. publicclassclsReceiver2extendsBroadcastReceiver{
  15. @Override
  16. publicvoidonReceive(Contextcontext,Intentintent){
  17. Stringaction=intent.getAction();
  18. Toast.makeText(context,"静态:"+action,1000).show();
  19. }
  20. }

你可能感兴趣的:(Android提高第六篇之BroadcastReceiver)