二.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{
-
-
protected static final String ACTION = "com.android.broadcast.RECEIVER_ACTION";
-
-
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=new Intent();
-
- 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./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./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{
-
-
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);
-
- registerReceiver(receiver, filter);
- }
- });
-
- unregisterReceiver=(Button)findViewById(R.id.btnunregisterReceiver);
-
-
unregisterReceiver.setOnClickListener(new OnClickListener(){
-
@Override
-
public void onClick(View v){
-
- 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./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)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。