Broastcast是应用程序间通信的手段。BroastcastReceiver也是跟Intent紧密相连的,动态/静态注册了BroastcastReceiver之后,使用sendBroadcast把Intent发送之后,系统会自动把符合条件的BroastcastReceiver启动,跟嵌入式系统的中断类似。
本文主要演示了如何静态/动态注册BroastcastReceiver,向系统索取电量信息,以及枚举信息的字段。
- <receiverandroid:name="clsReceiver2">
- <intent-filter>
- <action
- android:name="com.testBroadcastReceiver.Internal_2"/>
- </intent-filter>
- </receiver>
这是在AndroidManifest.xml 中添加接收器
布局文件:
01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:orientation="vertical" android:layout_width="fill_parent" 04. android:layout_height="fill_parent"> 05. 06. <Button android:id="@+id/Button01" android:layout_width="wrap_content" 07. android:layout_height="wrap_content" android:text="发送至内部动态注册的BroadcastReceiver"></Button> 08. <Button android:id="@+id/Button02" android:layout_width="wrap_content" 09. android:layout_height="wrap_content" android:text="发送至内部静态注册BroadcastReceiver"></Button> 10. <Button android:id="@+id/Button03" android:layout_width="wrap_content" 11. android:layout_height="wrap_content" android:text="发送至系统BroadcastReceiver"></Button> 12.</LinearLayout>
testBroadcastReceiver.java的代码如下:
01.package com.testBroadcastReceiver; 02. 03.import android.app.Activity; 04.import android.content.BroadcastReceiver; 05.import android.content.Context; 06.import android.content.Intent; 07.import android.content.IntentFilter; 08.import android.os.Bundle; 09.import android.util.Log; 10.import android.view.View; 11.import android.widget.Button; 12.import android.widget.Toast; 13. 14.public class testBroadcastReceiver extends Activity { 15. Button btnInternal1,btnInternal2,btnSystem; 16. static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1"; 17. static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2"; 18. static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3"; 19. @Override 20. public void onCreate(Bundle savedInstanceState) { 21. super.onCreate(savedInstanceState); 22. setContentView(R.layout.main); 23. btnInternal1=(Button)this.findViewById(R.id.Button01); 24. btnInternal1.setOnClickListener(new ClickEvent()); 25. btnInternal2=(Button)this.findViewById(R.id.Button02); 26. btnInternal2.setOnClickListener(new ClickEvent()); 27. btnSystem=(Button)this.findViewById(R.id.Button03); 28. btnSystem.setOnClickListener(new ClickEvent()); 29. //动态注册广播消息 30. registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1)); 31. } 32. class ClickEvent implements View.OnClickListener{ 33. 34. @Override 35. public void onClick(View v) { 36. if(v==btnInternal1)//给动态注册的BroadcastReceiver发送数据 37. { 38. Intent intent = new Intent(INTENAL_ACTION_1); 39. sendBroadcast(intent); 40. } 41. else if(v==btnInternal2)//给静态注册的BroadcastReceiver发送数据 42. { 43. Intent intent = new Intent(INTENAL_ACTION_2); 44. sendBroadcast(intent); 45. } 46. else if(v==btnSystem)//动态注册 接收2组信息的BroadcastReceiver 47. { 48. IntentFilter filter = new IntentFilter();// 49. filter.addAction(Intent.ACTION_BATTERY_CHANGED);//系统电量检测信息 50. filter.addAction(INTENAL_ACTION_3);//第三组自定义消息 51. registerReceiver(batInfoReceiver, filter); 52. 53. Intent intent = new Intent(INTENAL_ACTION_3); 54. intent.putExtra("Name", "hellogv"); 55. intent.putExtra("Blog", "http://blog.youkuaiyun.com/hellogv"); 56. sendBroadcast(intent);//传递过去 57. } 58. } 59. 60. } 61. 62. /* 63. * 接收动态注册广播的BroadcastReceiver 64. */ 65. private BroadcastReceiver bcrIntenal1 = new BroadcastReceiver() { 66. 67. public void onReceive(Context context, Intent intent) { 68. String action = intent.getAction(); 69. Toast.makeText(context, "动态:"+action, 1000).show(); 70. } 71. }; 72. 73. 74. private BroadcastReceiver batInfoReceiver = new BroadcastReceiver() { 75. 76. public void onReceive(Context context, Intent intent) { 77. String action = intent.getAction(); 78. //如果捕捉到的action是ACTION_BATTERY_CHANGED 79. if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 80. //当未知Intent包含的内容,则需要通过以下方法来列举 81. Bundle b=intent.getExtras(); 82. Object[] lstName=b.keySet().toArray(); 83. 84. for(int i=0;i<lstName.length;i++) 85. { 86. String keyName=lstName[i].toString(); 87. Log.e(keyName,String.valueOf(b.get(keyName))); 88. } 89. } 90. //如果捕捉到的action是INTENAL_ACTION_3 91. if (INTENAL_ACTION_3.equals(action)) { 92. //当未知Intent包含的内容,则需要通过以下方法来列举 93. Bundle b=intent.getExtras(); 94. Object[] lstName=b.keySet().toArray(); 95. 96. for(int i=0;i<lstName.length;i++) 97. { 98. String keyName=lstName[i].toString(); 99. Log.e(keyName,b.getString(keyName)); 100. } 101. } 102. } 103. }; 104. 105. 106.}
clsReceiver2.java的代码如下:
01.package com.testBroadcastReceiver; 02. 03.import android.content.BroadcastReceiver; 04.import android.content.Context; 05.import android.content.Intent; 06.import android.widget.Toast; 07. 08./* 09. * 接收静态注册广播的BroadcastReceiver, 10. * step1:要到AndroidManifest.xml这里注册消息 11. * <receiver android:name="clsReceiver2"> 12. <intent-filter> 13. <action 14. android:name="com.testBroadcastReceiver.Internal_2"/> 15. </intent-filter> 16. </receiver> 17. step2:定义消息的字符串 18. step3:通过Intent传递消息来驱使BroadcastReceiver触发 19. */ 20.public class clsReceiver2 extends BroadcastReceiver{ 21. @Override 22. public void onReceive(Context context, Intent intent) { 23. String action = intent.getAction(); 24. Toast.makeText(context, "静态:"+action, 1000).show(); 25. 26. } 27.}
http://blog.youkuaiyun.com/hellogv/archive/2010/11/10/5999170.aspx
本文展示了如何在Android应用中实现动态/静态注册BroadcastReceiver进行应用间通信,并通过发送Intent获取系统电量信息,同时介绍了如何枚举并处理Intent中的字段。
505

被折叠的 条评论
为什么被折叠?



