一提起廣播,咱們首先想到的是收音機,當咱們想要收聽某個廣播臺時只須要將收音機的頻率調至廣播臺所在的頻率便可!而Android中的廣播其實和收音機很是類似,不過它沒有所謂的頻率,它是由系統廣播一個事件,而後由其餘知足某一條件的程序接收並處理這個事件!!android
要在Android中實現廣播,首先咱們要在Manifest.xml文件中配置一個<receiver/>標籤,這個標籤必須有一個android:name屬性,值爲繼承自BroadcastReceiver類的接收器類!這個標籤還有一個子標籤爲<intent-filter/>,這個標籤很重要,是指定接收器須要接收哪一種廣播。另外,還有配置一個用戶權限:<uses-permission/>,具體的值能夠參考官方API文檔。web
另一個比較重要的步驟是必須有一個類繼承自BroadcastReceiver類,並複寫onReceiver方法,在該方法中處理接收到廣播後須要處理的事情!app
下面來看一個具體的例子,有助於更好的理解廣播機制是怎麼一回事。ide
UI部分就不說了,Activity上就加了一個按鈕,點擊後發送廣播。接收器接收到廣播後在終端輸出一句話。this
首先看AndroidManifest.xml文件:spa
<?xml version="1.0" encoding="utf-8"?>orm
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xml
package="com.gufengxiachen.broadcast"繼承
android:versionCode="1"事件
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadCast"
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=".MyBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.EDIT"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
下面是Activity:
package com.gufengxiachen.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 BroadCast extends Activity {
/** Called when the activity is first created. */
private Button bound =null;
private Button unbound =null;
private Button sendBroadCast =null;
private SecondBroadCastReceiver sbr =null;
public static final String SMS_ACTION="android.provider.Telephony.SMS_RECEVIER";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bound = (Button)findViewById(R.id.bound);
unbound = (Button)findViewById(R.id.unbound);
sendBroadCast = (Button)findViewById(R.id.sendBroadCast);
bound.setOnClickListener(new BoundListener());
unbound.setOnClickListener(new UnboundListener());
sendBroadCast.setOnClickListener(new SendBroadCast());
}
class SendBroadCast implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_EDIT);
BroadCast.this.sendBroadcast(intent);
}
}
class BoundListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sbr = new SecondBroadCastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(SMS_ACTION);
BroadCast.this.registerReceiver(sbr, filter);
}
}
class UnboundListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BroadCast.this.unregisterReceiver(sbr);
}
}
}
最後是接收器類:
package com.gufengxiachen.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("message receiver");
}
}