.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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:label="@string/app_name" android:name=".MainActivity" > <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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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) 的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。