读书人

BroadcastReceiver简介跟用法实例两

发布时间: 2012-06-27 14:20:08 作者: rapoo

BroadcastReceiver简介和用法实例,两种注册方式 (二)

一.BroadcastReceiver简介?

???BraodcastReceiver(广播接收器)是为了实现系统广播而提供的一种组件,它和事件处理机制类似,但是事件处理机制是程序组件级别的,而广播事件处理机制是系统级别的。比如,我们可以发出一种广播来测试手机电量的变化,这时候就可以定义一个BraodcastReceiver来接受广播,当手机电量较低时提示用户。我们既可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现BroadcastReceiver来监听和响应广播的Intent。

?? 在程序中使用BraodcastReceiver是比较简单的。首先要定义一个类继承BraodcastReceiver,并且覆盖onReceiver()方法来响应事件。然后注册在程序中BraodcastReceiver。最后构建Intent对象调用sendBroadcast()方法将广播发出。

二.BroadcastReceiver的注册方式

?1.静态注册方式

???静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action。静态注册方式的特点:不管改应用程序是否处于活动状态,都会进行监听,比如某个程序时监听内存?的使用情况的,当在手机上安装好后,不管改应用程序是处于什么状态,都会执行改监听方法中的内容。

下面是具体的例子:

MainActivity.java

  1. package?com.android.broadcast; ?
  2. ?
  3. import?android.app.Activity; ?
  4. import?android.content.Intent; ?
  5. import?android.os.Bundle; ?
  6. import?android.view.View; ?
  7. import?android.view.View.OnClickListener; ?
  8. import?android.widget.Button; ?
  9. ?
  10. public?class?MainActivity?extends?Activity{ ?
  11. ????//定义action常量 ?
  12. ????protected?static?final?String?ACTION?=?"com.android.broadcast.RECEIVER_ACTION"; ?
  13. ????//定义Button对象 ?
  14. ????private?Button?btnBroadcast; ?
  15. ????@Override?
  16. ????public?void?onCreate(Bundle?savedInstanceState){ ?
  17. ????????super.onCreate(savedInstanceState); ?
  18. ????????setContentView(R.layout.main); ?
  19. ????????btnBroadcast=(Button)findViewById(R.id.btnBroadcast); ?
  20. ????????//为按钮设置单击监听器 ?
  21. ????????btnBroadcast.setOnClickListener(new?OnClickListener(){ ?
  22. ????????????@Override?
  23. ????????????public?void?onClick(View?v){ ?
  24. ????????????????//实例化Intent ?
  25. ????????????????Intent?intent=new?Intent(); ?
  26. ????????????????//设置Intent的action属性 ?
  27. ????????????????intent.setAction(ACTION); ?
  28. ????????????????//发出广播 ?
  29. ????????????????sendBroadcast(intent); ?
  30. ????????????} ?
  31. ????????}); ?
  32. ????} ?
  33. }?

在“com.android.broadcast”包中定义一个MyReceiver类,继承于BroadcastReceiver,覆盖onReceive()方法。

MyReceiver.java

  1. package?com.android.broadcast; ?
  2. ?
  3. import?android.content.BroadcastReceiver; ?
  4. import?android.content.Context; ?
  5. import?android.content.Intent; ?
  6. import?android.util.Log; ?
  7. ?
  8. public?class?MyReceiver?extends?BroadcastReceiver{ ?
  9. ???//定义日志标签 ?
  10. ????private?static?final?String?TAG?=?"Test"; ?
  11. ????@Override?
  12. ????public?void?onReceive(Context?context,?Intent?intent){ ?
  13. ????????//输出日志信息 ?
  14. ????????Log.i(TAG,?"MyReceiver?onReceive--->"); ?
  15. ????} ?
  16. } ?

main.xml

  1. <?xml?version="1.0"?encoding="utf-8"?>?
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
  3. ????android:orientation="vertical"?
  4. ????android:layout_width="fill_parent"?
  5. ????android:layout_height="fill_parent"?
  6. ????>?
  7. ????<Button?
  8. ????????android:id="@+id/btnBroadcast"?
  9. ????????android:layout_width="match_parent"?
  10. ????????android:layout_height="wrap_content"?
  11. ????????android:text="发送Broadcast"?
  12. ????????/>?
  13. </LinearLayout>?

在AndroidManifest.xml配置文件中16~20行声明receiver

  1. <?xml?version="1.0"?encoding="utf-8"?>?
  2. <manifest?xmlns:android="http://schemas.android.com/apk/res/android"?
  3. ??????package="com.android.broadcast"?
  4. ??????android:versionCode="1"?
  5. ??????android:versionName="1.0">?
  6. ????<uses-sdk?android:minSdkVersion="10"?/>?
  7. ?
  8. ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">?
  9. ????????<activity?android:name=".MainActivity"?
  10. ??????????????????android:label="@string/app_name">?
  11. ????????????<intent-filter>?
  12. ????????????????<action?android:name="android.intent.action.MAIN"?/>?
  13. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>?
  14. ????????????</intent-filter>?
  15. ????????</activity>?
  16. ????????<receiver?android:name="MyReceiver">?
  17. ????????????<intent-filter>?
  18. ????????????????<action?android:name="com.android.broadcast.RECEIVER_ACTION"/>?
  19. ????????????</intent-filter>?
  20. ????????</receiver>?
  21. ????</application>?
  22. </manifest>?

效果图:

?BroadcastReceiver简介跟用法实例,两种注册方式 (二)

?当我们点击BroadcastReceiver简介跟用法实例,两种注册方式 (二)按钮的时候,程序会调用onReceive()方法,LogCat输出信息如下:

BroadcastReceiver简介跟用法实例,两种注册方式 (二)

?2.动态注册方式

?? 动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action。动态注册方式特点:在代码中进行注册后,当应用程序关闭后,就不再进行监听。

下面是具体的例子:

?MainActivity.java

  1. package?com.android.broadcast; ?
  2. ?
  3. import?android.app.Activity; ?
  4. import?android.content.Intent; ?
  5. import?android.content.IntentFilter; ?
  6. import?android.os.Bundle; ?
  7. import?android.view.View; ?
  8. import?android.view.View.OnClickListener; ?
  9. import?android.widget.Button; ?
  10. ?
  11. public?class?MainActivity?extends?Activity{ ?
  12. ????//定义Action常量 ?
  13. ????protected?static?final?String?ACTION?=?"com.android.broadcast.RECEIVER_ACTION"; ?
  14. ????private?Button?btnBroadcast; ?
  15. ????private?Button?registerReceiver; ?
  16. ????private?Button?unregisterReceiver; ?
  17. ????private?MyReceiver?receiver; ?
  18. ????@Override?
  19. ????public?void?onCreate(Bundle?savedInstanceState){ ?
  20. ????????super.onCreate(savedInstanceState); ?
  21. ????????setContentView(R.layout.main); ?
  22. ????????btnBroadcast=(Button)findViewById(R.id.btnBroadcast); ?
  23. ????????//创建事件监听器 ?
  24. ????????btnBroadcast.setOnClickListener(new?OnClickListener(){ ?
  25. ????????????@Override?
  26. ????????????public?void?onClick(View?v){ ?
  27. ????????????????Intent?intent=new?Intent(); ?
  28. ????????????????intent.setAction(ACTION); ?
  29. ????????????????sendBroadcast(intent); ?
  30. ????????????} ?
  31. ????????}); ?
  32. ???????? ?
  33. ????????registerReceiver=(Button)findViewById(R.id.btnregisterReceiver); ?
  34. ????????//创建事件监听器 ?
  35. ????????registerReceiver.setOnClickListener(new?OnClickListener(){ ?
  36. ????????????@Override?
  37. ????????????public?void?onClick(View?v){ ?
  38. ????????????????receiver=new?MyReceiver(); ?
  39. ????????????????IntentFilter?filter=new?IntentFilter(); ?
  40. ????????????????filter.addAction(ACTION); ?
  41. ????????????????//动态注册BroadcastReceiver ?
  42. ????????????????registerReceiver(receiver,?filter); ?
  43. ????????????} ?
  44. ????????}); ?
  45. ???????? ?
  46. ????????unregisterReceiver=(Button)findViewById(R.id.btnunregisterReceiver); ?
  47. ????????//创建事件监听器 ?
  48. ????????unregisterReceiver.setOnClickListener(new?OnClickListener(){ ?
  49. ????????????@Override?
  50. ????????????public?void?onClick(View?v){ ?
  51. ????????????????//注销BroadcastReceiver ?
  52. ????????????????unregisterReceiver(receiver); ?
  53. ????????????} ?
  54. ????????}); ?
  55. ????} ?
  56. }?

?在“com.android.broadcast”包中定义一个MyReceiver类,继承于BroadcastReceiver,覆盖onReceive()方法。

MyReceiver.java

  1. package?com.android.broadcast; ?
  2. ?
  3. import?android.content.BroadcastReceiver; ?
  4. import?android.content.Context; ?
  5. import?android.content.Intent; ?
  6. import?android.util.Log; ?
  7. ?
  8. public?class?MyReceiver?extends?BroadcastReceiver{ ?
  9. ????//定义日志标签 ?
  10. ????private?static?final?String?TAG?=?"Test"; ?
  11. ????@Override?
  12. ????public?void?onReceive(Context?context,?Intent?intent){ ?
  13. ????????//输出日志信息 ?
  14. ????????Log.i(TAG,?"MyReceiver?onReceive--->"); ?
  15. ????} ?
  16. } ?

main.xml

  1. <?xml?version="1.0"?encoding="utf-8"?>?
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
  3. ????android:orientation="vertical"?
  4. ????android:layout_width="fill_parent"?
  5. ????android:layout_height="fill_parent"?
  6. ????>?
  7. ????<Button?
  8. ????????android:id="@+id/btnBroadcast"?
  9. ????????android:layout_width="match_parent"?
  10. ????????android:layout_height="wrap_content"?
  11. ????????android:text="发送广播"?
  12. ????????/>?
  13. ????<Button?
  14. ????????android:id="@+id/btnregisterReceiver"?
  15. ????????android:layout_width="match_parent"?
  16. ????????android:layout_height="wrap_content"?
  17. ????????android:text="注册广播接收器"?
  18. ????????/>?
  19. ????<Button?
  20. ????????android:id="@+id/btnunregisterReceiver"?
  21. ????????android:layout_width="match_parent"?
  22. ????????android:layout_height="wrap_content"?
  23. ????????android:text="注销广播接听器"?
  24. ????????/>?
  25. </LinearLayout>?

效果图:

BroadcastReceiver简介跟用法实例,两种注册方式 (二)?

①当我们首先点击BroadcastReceiver简介跟用法实例,两种注册方式 (二)按钮的时候,因为程序没有注册BraodcastReceiver,所以LogCat没有输出任何信息。

②当我们先点击BroadcastReceiver简介跟用法实例,两种注册方式 (二)再点击BroadcastReceiver简介跟用法实例,两种注册方式 (二)按钮的时候,这时程序会动态的注册BraodcastReceiver,之后会调用onReceive()方法,LogCat输出信息如下:

BroadcastReceiver简介跟用法实例,两种注册方式 (二)

?

?③当我们点击BroadcastReceiver简介跟用法实例,两种注册方式 (二)按钮的时候,这时程序会注销BraodcastReceiver,再点击BroadcastReceiver简介跟用法实例,两种注册方式 (二),LogCat没有输出任何信息。

三.BroadcastReceiver?的生命周期

???一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。

?

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/655885

读书人网 >移动开发

热点推荐