主要内容:
(1)广播机制介绍
(2)BroadcastReceiver实现
(3)BroadcastReceiver操作
(4)BroadcastReceiver生命周期
(5)注册BroadcastReceiver的方法
(6)Android的一些内置的BroadcastReceiver
具体讲解:
(一)广播机制介绍
广播机制,个人理解就是:广播方不管收听方做什么操作,只管广播,我们要做的就是收听到广播之后进行哪些操作。
(二)BroadcastReceiver实现
看代码:
package com.wjl;
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 TestActivity extends Activity {
private Button sendButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendButton = (Button)findViewById(R.id.sendButton);
sendButton.setOnClickListener(new BroadcastListener());
}
class BroadcastListener implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
TestActivity.this.sendBroadcast(intent);
}
}
}
(三)BoadcastReceiver操作
package com.wjl;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TestReceiver extends BroadcastReceiver {
public TestReceiver(){
System.out.println("receiver");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceiver");
}
}
需要注意的是:
需要在androidManifest.xml注册一下:
<receiver
android:name=".TestReceiver">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
(四)BroadcastReceiver生命周期
当点击按钮之后intent会到AndroidManifest.xml文件中找是不是有匹配的action,如果有就会调用TestReceiver ,然后获得TestReceiver 对象,再执行onReceive方法,这时候TestReceiver 对象就没有用了,当我们再次点击按钮的时候就会重新获得对象,这就是BroadcastReceiver的生命周期;
(五)注册BroadcastReceiver的方法
注册BroadcastReceiver的方法有两种,上面介绍了一种是在AndroidManifest.xml文件中注册的,还有一种是在类中注册的,方法如下:
package com.wjl;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BroadcastReceiverActivity extends Activity {
private Button registerButton = null;
private Button unregisterButton = null;
private MSMReceiver msmReceiver = null;
private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerButton = (Button)findViewById(R.id.registerButton);
registerButton.setOnClickListener(new RegisterButtonListener());
unregisterButton = (Button)findViewById(R.id.unregisterButton);
unregisterButton.setOnClickListener(new UnRegisterButtonListener());
}
class RegisterButtonListener implements OnClickListener{
public void onClick(View v) {
//生成一个BroadcastReceive对象
msmReceiver = new MSMReceiver();
//生成一个IntentFilter对象
IntentFilter intentFilter = new IntentFilter();
//设置IntentFilter对象的action
intentFilter.addAction(SMS_ACTION);
//将BroadcastReceive对象注册到系统当中
BroadcastReceiverActivity.this.registerReceiver(msmReceiver, intentFilter);
}
}
class UnRegisterButtonListener implements OnClickListener{
public void onClick(View v) {
//解除BroadcastReceive在系统当中的注册
BroadcastReceiverActivity.this.unregisterReceiver(msmReceiver);
}
}
}
红色部分
(六)Android的一些内置的BroadcastReceiver
private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
值得注意的是,有个权限还得设置一下:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
MSMReceiver :
package com.wjl;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class MSMReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Receive message");
//接受content对象中的数据
Bundle bundle = intent.getExtras();
//在bundle对象当中有一个属性名字为pdus,这个属性的值是一个object数组
Object [] myOBJpdus = (Object[])bundle.get("pdus");
//创建SmsMessage类型的数组
SmsMessage [] message = new SmsMessage[myOBJpdus.length];
System.out.println(message.length);
for(int i=0;i<myOBJpdus.length;i++){
//使用Object数组当中的对象创建SmsMessage对象
message[i] = SmsMessage.createFromPdu((byte[])myOBJpdus[i]);
//调用SmsMessage对象的getMessageBody()方法得到消息内容
System.out.println(message[i].getMessageBody());
}
}
}
在Android 中如果要发送一个广播必须使用sendBroadCast 向系统发送对其感兴趣的广播接收器中。
使用广播必须要有一个 intent 对象必设置其 action 动作对象使用广播必须在配置文件中显式的指明该广播对象
每次接收广播都会重新生成一个接收广播的对象
在 BroadCast 中尽量不要处理太多逻辑问题,建议复杂的逻辑交给 Activity 或者 Service 去处理