Broadcast Receiver用于接收并处理广播通知(broadcastannouncements)。
多数的广播是系统发起的,如地域变换、电量不足、来电来信等。
程序可以有任意数量的Broadcast Receivers来响应它觉得重要的通知。
Broadcast Receiver可以通过多种方式通知用户:启动activity、使用NotificationManager、开启背景灯、振动设备、播放声音等,最典型的是在状态栏显示一个图标,这样用户就可以点它打开看通知内容。
通常我们的某个应用或系统本身在某些事件(电池电量不足、来电来短信)来临时会广播一个Intent出去,我们可以利用注册一个Broadcast Receiver来监听到这些Intent并获取Intent中的数据。
一、在程序中接受系统发的广播
我们举一个例子说明,一个接受系统Date改变的广播的例子。
先新建一个HelloBroadcastReceiver.java类,继承自BroadcastReceiver并复写它的onReceive方法。这样就创建了一个专门用来接收广播的类。
package com.tianjf; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class HelloBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("debug", "Date has been changed!"); Log.d("debug", ""+intent.getAction()); } }
但是上面的Broadcast Receiver没有指定接收哪个广播,我们要指定某个Receiver接收哪个广播,必须在AndroidManifest.xml中注册此Receiver<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tianjf" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".BroadcastDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 定义Broadcast Receiver指定监听的Action(系统启动action) --> <receiver android:name="HelloBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.DATE_CHANGED" /> </intent-filter> </receiver> </application> </manifest>
OK,运行之后,更改系统Date看看,果然打印了两行Log。(着两行Log有时候打得出来,有时候打不出来,不知道神马原因)
二、接收自己发的广播
直接上代码
BroadcastDemoActivity.java
package com.tianjf; 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 BroadcastDemoActivity extends Activity implements OnClickListener { private Button button; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { // 定义一个intent Intent intent = new Intent(); intent.setAction("MY_BROADCAST_ACTION"); intent.putExtra("yaoyao", "yaoyao is 189 days old ,27 weeks -- 2010-08-10"); // 广播出去 sendBroadcast(intent); } }
HelloBroadcastReceiver.javapackage com.tianjf; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class HelloBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("debug", "" + intent.getAction()); if (intent.getAction().equals("android.intent.action.DATE_CHANGED")) { Log.d("debug", "Date has been changed!"); Log.d("debug", "" + intent.getAction()); } else if (intent.getAction().equals("MY_BROADCAST_ACTION")) { Log.d("debug", "Say Hello to Yaoyao!"); Log.d("debug", intent.getStringExtra("yaoyao")); } } }
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tianjf" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".BroadcastDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 定义Broadcast Receiver指定监听的Action(系统启动action和自动义的广播) --> <receiver android:name="HelloBroadcastReceiver" > <intent-filter> <action android:name="MY_BROADCAST_ACTION" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.DATE_CHANGED" /> </intent-filter> </receiver> </application> </manifest>