Android 的广播机制
一听到广播我们第一感觉就会联想到小时候村里面的广播,每逢村里有什么活动都是通过广播发送的。收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同。接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理。好比我们收听交通电台的广播,电台中告诉我们现在在交通状况如何,但它并不关心我们接收到广播时做如何做出处理,这不是广播应该关心的问题。
图解:
BroadcastReceiver监听被系统广播的事件,所以BroadcastReceiver需要在系统中注册。注册的方式有两种:
1.在AndroidManifest.xml文件中进行注册
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
2.在应用程序的代码中进行注册
注册BroadcastReceiver:registerReceiver(receiver,filter);
取消注册BroadcastReceiver:unregisterReceiver(receiver);
两者之间的区别:在AndroidManifest.xml文件中注册的广播接收器,即使应用程序没有启动或者关闭了,也处于活动状态。当有广播时系统仍然后启动这个广播接收器进行处理。在应用程序的代码中注册的广播接收器只有到应用程序正在执行时的时候注册,当要关闭应用程序时可以取消注册,更加的灵活。如果我们的广播接收器接收的广播用来更新Activity的界面时应该采用这种方式注册BroadcastReceiver。
示例:新建一个应用程序项目,在main.xml文件中添加两个Button按钮,点击之后分别注册广播接收器和取消注册广播接收器。注册广播接收器之后当我们向模拟器发送短消息时,打印出短消息的内容。由于在这个程序中广播接收器需要接收短信的广播,所以需要在AndroidManifest.xml文件添加接收短消息广播事件的权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 。
源代码:
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:text="注册广播接收器"
android:id="@+id/buttonRegister"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
<Button
android:text="取消注册的广播接收器"
android:id="@+id/buttonUnregister"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
BR_Activity.java
package idea.org;
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 BR_Activity extends Activity {
private Button buttonRegister=null;
private Button buttonUnregister=null;
MyBroadcastReceiver myBroadcastReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonRegister=(Button)findViewById(R.id.buttonRegister);
//为button按钮添加监听器
buttonRegister.setOnClickListener(new OnClickListener()
{
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//生成广播接收器对象
myBroadcastReceiver=new MyBroadcastReceiver();
//生成一个IntentFilter过滤器对象
IntentFilter filter=new IntentFilter();
//为IntentFilter添加一个Action
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
//将广播接收器注册到系统当中
BR_Activity.this.registerReceiver(myBroadcastReceiver, filter);
}
});
buttonUnregister=(Button)findViewById(R.id.buttonUnregister);
buttonUnregister.setOnClickListener(new OnClickListener(){
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//接触广播接收器的注册
BR_Activity.this.unregisterReceiver(myBroadcastReceiver);
}
});
}
}
MyBroadcastReceiver.java
package idea.org;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class MyBroadcastReceiver extends BroadcastReceiver{
//复写onReceive()方法,在onReceive中对接收到的广播进行处理
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Receive Message");
//接收Intent对象中的数据
Bundle bundle=intent.getExtras();
Object []myOBJpdus=(Object[])bundle.get("pdus");
//生成SmsMessage对象数组
SmsMessage[] messages=new SmsMessage[myOBJpdus.length];
System.out.println("收到"+messages.length+"条短信");
for(int i=0;i<messages.length;i++)
{
messages[i]=SmsMessage.createFromPdu((byte[])myOBJpdus[i]);
System.out.println(messages[i].getDisplayMessageBody());
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="idea.org"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="BR_Activity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!--为应用程序添加添加RECEIVE_SMS权限,从而使应用程序有收到SMS Intent广播的权限 -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>