BroadcastReceiver用于监听被广播事件(Intent)为了达到这个目的,BroadcastReceiver必须进行注册,注册有一下两个方法:
1.在应用程序的代码当中进行注册
注册BroadcastReceiver:registerReceiver(receiver,filter);//其中filter作用和 <intent-filter>标签一样。
取消注册BroadcastReceiver:unregisterReceiver(receiver);
如果一个BroadcastReceiver用于更新UI,那么通常会使用这种方法进行注册,
在Activity启动的时候注册BroadcastReceiver,在Activity不可见以后取消注册。
2.在androidManifest.xml中进行注册(这种方法,即使应用程序关闭了,都可以接收到广播事件的,始终处于活动状态)
<receiver android:name=".TestReceiver">
<intent-filter><!-- 决定接受什么类型的Receiver -->
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</receiver>
这里还需要注意一点:在android的较高版本中(估计是4.0后)在使用监听短信时需要授权,在mainframe.xml中加入如下代码,便可以解决广播后无法进入onReceive方法。
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
==============MainActivity.java====================
package com.yx.testbc2;
import android.os.Bundle;
import android.app.Activity;
import android.content.IntentFilter;
import android.view.LayoutInflater.Filter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button registerReceiver;
private Button unregisterReceiver;
private SMSReceiver smsReceiver=null;
private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver = (Button) findViewById(R.id.registerReceiver);
unregisterReceiver = (Button) findViewById(R.id.unregisterReceiver);
registerReceiver.setOnClickListener(new registerReceiverListener());
unregisterReceiver.setOnClickListener(new unregisterReceiverListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class registerReceiverListener implements OnClickListener{
@Override
public void onClick(View arg0) {
//生成一个BroadcastReceiver对象
smsReceiver = new SMSReceiver();
//生成一个IntentFilter过滤器对象
IntentFilter filter = new IntentFilter();
//为IntentFilter添加一个action
filter.addAction(SMS_ACTION);
//将BroadcastReceiver对象注册到系统中
MainActivity.this.registerReceiver(smsReceiver, filter);
}
}
class unregisterReceiverListener implements OnClickListener{
@Override
public void onClick(View arg0) {
//解除BroadcastReceiver
MainActivity.this.unregisterReceiver(smsReceiver);
}
}
}
=================SMSReceiver.java=================
package com.yx.testbc2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
System.out.println("SMSReceiver---onReceive");
//接受intent当中的数据
Bundle bundle = intent.getExtras();
//在Bundle对象当中有个叫"pdus"的属性,这个属性的值是一个Objec数组
Object[] myBJpdus = (Object[]) bundle.get("pdus");
//创建一个SmsMessage类型的数组
SmsMessage[] message = new SmsMessage[myBJpdus.length];
System.out.println(message.length);
for (int i = 0; i < myBJpdus.length; i++) {
//使用Object数组当中的对象创建SmsMessgae对象
message[i] = SmsMessage.createFromPdu((byte[]) myBJpdus[i]);
System.out.println(message[i].getMessageBody());
}
}
}
===================AndroidManifest.xml=================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yx.testbc2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yx.testbc2.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>
</application>
</manifest>