Android之旅六 BroadcastReceiver
1、 广播:发送方只管发送数据,而不关心数据是否被接收方接收以及接收方接收数据后是如何处理的。2、 Android广播流程:写一个类,继承BroadcastReceiver,覆写里面的onReceive方法,然后在AndroidManifest.xml中进行配置,
配置中有个<intent-filter>标签,用来过滤用户请求过来的广播,用户通过sendBroadcast发送广播对象,接收一个Intent参数对象,该
Intent参数需要指定Action,以便在<intent-filter>接收,然后到相应的广播类中进行处理。
[java]
package com.broadcast.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcast extends BroadcastReceiver{
public MyBroadcast(){
System.out.println("MyBroadcast");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive is start");
}
}
在AndroidMainifest.xml中进行配置:
[java]
<receiver android:name=".MyBroadcast">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
测试广播类:
[java]
public class MainActivity extends Activity {
Button button=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.broad);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
//与AndroidManifest.xml中的intent-filter是否匹配
intent.setAction(Intent.ACTION_EDIT);//android.intent.action.EDIT
sendBroadcast(intent);//发送广播
}
});
}
}
3、 每次接收广播,都会生成一个新的BroadcastReceiver对象,如果我们自定义Broadcast继承BroadcastReceiver,那么每次广播
,都会生成新的对象,执行完onReceive处理完后,这个对象将不会再使用了。
4、 BroadcastReceiver用于监听被广播的事件,这种事件一般是通过Intent的方式来接收的,为了达到这个目的,
BroadcastReceiver必须进行注册,注册的方法有两种:
1)在应用程序的代码当中进行注册
registerReceiver(receiver,IntentFilter);
取消注册:unregisterReceiver(receiver);
这种方式比较灵活,我们需要的时候就可以进行注册,不需要的时候将其关闭即可,例如一个BroadcastReceiver用于更新UI,通常会使用这种
方式进行注册,在Activity启动的时候注册,在Activity不可见以后取消注册。
定义BroadcastReceiver类:
[java
package com.broad.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SMSReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("receive message");
//处理短信信息
// Bundle bundle=intent.getExtras();
// Object[] myOBJpdus=(Object[])bundle.get("pdus");
// SmsMessage[] message=new SmsMessage[myOBJpdus.length];
// System.out.println("message length="+message.length);
// for(int i=0;i<myOBJpdus.length;i++){
// message[i]=SmsMessage.createFromPdu((byte[])myOBJpdus[i]);
// System.out.println(message[i].getDisplayMessageBody());
// }
}
}
通过代码注册BroadcastReceiver:
[java]
/**
* 通过代码注册BroadcastReceiver
* @author Administrator
*
*/
public class MainActivity extends Activity {
private Button register=null;
private Button unregister=null;
private SMSReceiver smsReceiver=null;
private static final String SMS_ACTION="android.provider.Telephony.SMS_RECEIVED";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
register=(Button)findViewById(R.id.register);
register.setOnClickListener(new RegisterReceiverListener());
unregister=(Button)findViewById(R.id.unregister);
unregister.setOnClickListener(new UnRegisterReceiverListener());
}
class RegisterReceiverListener implements OnClickListener{
@Override
public void onClick(View v) {
smsReceiver=new SMSReceiver();
IntentFilter filter=new IntentFilter();
//为IntentFilter添加一个Action,相当于在AndroidMainifest.xml中的Intent-filter
filter.addAction(SMS_ACTION);
registerReceiver(smsReceiver, filter);
}
}
c
补充:移动开发 , Android ,