BroadcastReceiver简介和用法实例,两种注册方式 (二)
一.BroadcastReceiver简介?
???BraodcastReceiver(广播接收器)是为了实现系统广播而提供的一种组件,它和事件处理机制类似,但是事件处理机制是程序组件级别的,而广播事件处理机制是系统级别的。比如,我们可以发出一种广播来测试手机电量的变化,这时候就可以定义一个BraodcastReceiver来接受广播,当手机电量较低时提示用户。我们既可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现BroadcastReceiver来监听和响应广播的Intent。
?? 在程序中使用BraodcastReceiver是比较简单的。首先要定义一个类继承BraodcastReceiver,并且覆盖onReceiver()方法来响应事件。然后注册在程序中BraodcastReceiver。最后构建Intent对象调用sendBroadcast()方法将广播发出。
二.BroadcastReceiver的注册方式
?1.静态注册方式
???静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action。静态注册方式的特点:不管改应用程序是否处于活动状态,都会进行监听,比如某个程序时监听内存?的使用情况的,当在手机上安装好后,不管改应用程序是处于什么状态,都会执行改监听方法中的内容。
下面是具体的例子:
MainActivity.java
- package?com.android.broadcast; ?
- ?
- import?android.app.Activity; ?
- import?android.content.Intent; ?
- import?android.os.Bundle; ?
- import?android.view.View; ?
- import?android.view.View.OnClickListener; ?
- import?android.widget.Button; ?
- ?
- public?class?MainActivity?extends?Activity{ ?
- ????//定义action常量 ?
- ????protected?static?final?String?ACTION?=?"com.android.broadcast.RECEIVER_ACTION"; ?
- ????//定义Button对象 ?
- ????private?Button?btnBroadcast; ?
- ????@Override?
- ????public?void?onCreate(Bundle?savedInstanceState){ ?
- ????????super.onCreate(savedInstanceState); ?
- ????????setContentView(R.layout.main); ?
- ????????btnBroadcast=(Button)findViewById(R.id.btnBroadcast); ?
- ????????//为按钮设置单击监听器 ?
- ????????btnBroadcast.setOnClickListener(new?OnClickListener(){ ?
- ????????????@Override?
- ????????????public?void?onClick(View?v){ ?
- ????????????????//实例化Intent ?
- ????????????????Intent?intent=new?Intent(); ?
- ????????????????//设置Intent的action属性 ?
- ????????????????intent.setAction(ACTION); ?
- ????????????????//发出广播 ?
- ????????????????sendBroadcast(intent); ?
- ????????????} ?
- ????????}); ?
- ????} ?
- }?
在“com.android.broadcast”包中定义一个MyReceiver类,继承于BroadcastReceiver,覆盖onReceive()方法。
MyReceiver.java
- package?com.android.broadcast; ?
- ?
- import?android.content.BroadcastReceiver; ?
- import?android.content.Context; ?
- import?android.content.Intent; ?
- import?android.util.Log; ?
- ?
- public?class?MyReceiver?extends?BroadcastReceiver{ ?
- ???//定义日志标签 ?
- ????private?static?final?String?TAG?=?"Test"; ?
- ????@Override?
- ????public?void?onReceive(Context?context,?Intent?intent){ ?
- ????????//输出日志信息 ?
- ????????Log.i(TAG,?"MyReceiver?onReceive--->"); ?
- ????} ?
- } ?
main.xml
- <?xml?version="1.0"?encoding="utf-8"?>?
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
- ????android:orientation="vertical"?
- ????android:layout_width="fill_parent"?
- ????android:layout_height="fill_parent"?
- ????>?
- ????<Button?
- ????????android:id="@+id/btnBroadcast"?
- ????????android:layout_width="match_parent"?
- ????????android:layout_height="wrap_content"?
- ????????android:text="发送Broadcast"?
- ????????/>?
- </LinearLayout>?
在AndroidManifest.xml配置文件中16~20行声明receiver
- <?xml?version="1.0"?encoding="utf-8"?>?
- <manifest?xmlns:android="http://schemas.android.com/apk/res/android"?
- ??????package="com.android.broadcast"?
- ??????android:versionCode="1"?
- ??????android:versionName="1.0">?
- ????<uses-sdk?android:minSdkVersion="10"?/>?
- ?
- ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">?
- ????????<activity?android:name=".MainActivity"?
- ??????????????????android:label="@string/app_name">?
- ????????????<intent-filter>?
- ????????????????<action?android:name="android.intent.action.MAIN"?/>?
- ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>?
- ????????????</intent-filter>?
- ????????</activity>?
- ????????<receiver?android:name="MyReceiver">?
- ????????????<intent-filter>?
- ????????????????<action?android:name="com.android.broadcast.RECEIVER_ACTION"/>?
- ????????????</intent-filter>?
- ????????</receiver>?
- ????</application>?
- </manifest>?
效果图:
?
?当我们点击按钮的时候,程序会调用onReceive()方法,LogCat输出信息如下:
?2.动态注册方式
?? 动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action。动态注册方式特点:在代码中进行注册后,当应用程序关闭后,就不再进行监听。
下面是具体的例子:
?MainActivity.java
- package?com.android.broadcast; ?
- ?
- import?android.app.Activity; ?
- import?android.content.Intent; ?
- import?android.content.IntentFilter; ?
- import?android.os.Bundle; ?
- import?android.view.View; ?
- import?android.view.View.OnClickListener; ?
- import?android.widget.Button; ?
- ?
- public?class?MainActivity?extends?Activity{ ?
- ????//定义Action常量 ?
- ????protected?static?final?String?ACTION?=?"com.android.broadcast.RECEIVER_ACTION"; ?
- ????private?Button?btnBroadcast; ?
- ????private?Button?registerReceiver; ?
- ????private?Button?unregisterReceiver; ?
- ????private?MyReceiver?receiver; ?
- ????@Override?
- ????public?void?onCreate(Bundle?savedInstanceState){ ?
- ????????super.onCreate(savedInstanceState); ?
- ????????setContentView(R.layout.main); ?
- ????????btnBroadcast=(Button)findViewById(R.id.btnBroadcast); ?
- ????????//创建事件监听器 ?
- ????????btnBroadcast.setOnClickListener(new?OnClickListener(){ ?
- ????????????@Override?
- ????????????public?void?onClick(View?v){ ?
- ????????????????Intent?intent=new?Intent(); ?
- ????????????????intent.setAction(ACTION); ?
- ????????????????sendBroadcast(intent); ?
- ????????????} ?
- ????????}); ?
- ???????? ?
- ????????registerReceiver=(Button)findViewById(R.id.btnregisterReceiver); ?
- ????????//创建事件监听器 ?
- ????????registerReceiver.setOnClickListener(new?OnClickListener(){ ?
- ????????????@Override?
- ????????????public?void?onClick(View?v){ ?
- ????????????????receiver=new?MyReceiver(); ?
- ????????????????IntentFilter?filter=new?IntentFilter(); ?
- ????????????????filter.addAction(ACTION); ?
- ????????????????//动态注册BroadcastReceiver ?
- ????????????????registerReceiver(receiver,?filter); ?
- ????????????} ?
- ????????}); ?
- ???????? ?
- ????????unregisterReceiver=(Button)findViewById(R.id.btnunregisterReceiver); ?
- ????????//创建事件监听器 ?
- ????????unregisterReceiver.setOnClickListener(new?OnClickListener(){ ?
- ????????????@Override?
- ????????????public?void?onClick(View?v){ ?
- ????????????????//注销BroadcastReceiver ?
- ????????????????unregisterReceiver(receiver); ?
- ????????????} ?
- ????????}); ?
- ????} ?
- }?
?在“com.android.broadcast”包中定义一个MyReceiver类,继承于BroadcastReceiver,覆盖onReceive()方法。
MyReceiver.java
- package?com.android.broadcast; ?
- ?
- import?android.content.BroadcastReceiver; ?
- import?android.content.Context; ?
- import?android.content.Intent; ?
- import?android.util.Log; ?
- ?
- public?class?MyReceiver?extends?BroadcastReceiver{ ?
- ????//定义日志标签 ?
- ????private?static?final?String?TAG?=?"Test"; ?
- ????@Override?
- ????public?void?onReceive(Context?context,?Intent?intent){ ?
- ????????//输出日志信息 ?
- ????????Log.i(TAG,?"MyReceiver?onReceive--->"); ?
- ????} ?
- } ?
main.xml
- <?xml?version="1.0"?encoding="utf-8"?>?
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?
- ????android:orientation="vertical"?
- ????android:layout_width="fill_parent"?
- ????android:layout_height="fill_parent"?
- ????>?
- ????<Button?
- ????????android:id="@+id/btnBroadcast"?
- ????????android:layout_width="match_parent"?
- ????????android:layout_height="wrap_content"?
- ????????android:text="发送广播"?
- ????????/>?
- ????<Button?
- ????????android:id="@+id/btnregisterReceiver"?
- ????????android:layout_width="match_parent"?
- ????????android:layout_height="wrap_content"?
- ????????android:text="注册广播接收器"?
- ????????/>?
- ????<Button?
- ????????android:id="@+id/btnunregisterReceiver"?
- ????????android:layout_width="match_parent"?
- ????????android:layout_height="wrap_content"?
- ????????android:text="注销广播接听器"?
- ????????/>?
- </LinearLayout>?
效果图:
?
①当我们首先点击按钮的时候,因为程序没有注册BraodcastReceiver,所以LogCat没有输出任何信息。
②当我们先点击再点击
按钮的时候,这时程序会动态的注册BraodcastReceiver,之后会调用onReceive()方法,LogCat输出信息如下:
?
?③当我们点击按钮的时候,这时程序会注销BraodcastReceiver,再点击
,LogCat没有输出任何信息。
三.BroadcastReceiver?的生命周期
???一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。
?
本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/655885
