一、 BroadcastReceiver简介BroadcastReceiver,用于异步接收广播Intent,广播Intent是通过调用Context.sendBroadcast()发送、BroadcastReceiver()接收。广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()、Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似。广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码,如:时区改变的通知、电池电量不足、用户改变了语言偏好,或者开机启动等广播接收器没有用户界面,但是它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.BroadcastReceiver 接收广播方式:1. Normal broadcasts(正常广播),用 Context.sendBroadcast()发送是完全异步的,它们都运行在一个未定义的顺序,通常是在同一时间。这样会更有效,但意味着receiver不能包含所要使用的结果或中止的API。 2. Ordered broadcasts(有序广播),用 Context.sendOrderedBroadcast()发送每次被发送到一个receiver。所谓有序,就是每个receiver执行后可以传播到下一个receiver,也可以完全中止传播——不传播给其他receiver。 而receiver运行的顺序可以通过matched intent-filter 里面的android:priority来控制,当priority优先级相同的时候,Receiver以任意的顺序运行。二、 BroadcastReceiver注册方式1 静态注册AndroidManifest.xml中,application里面,定义receiver并设置要接收的action[java] view plaincopyprint? 2 动态注册Activity中,需在onStart()中调用registerReceiver()进行注册和在onStop中调用unregisterReceiver()释放服务[java] view plaincopyprint?private MusicReceiver receiver; @Override protected void onStart(){ super.onStart(); receiver = new MusicReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("com.homer.receiver.musicReceiver"); this.registerReceiver(receiver, filter); } @Override protected void onStop(){ this.unregisterReceiver(receiver); super.onStop(); } 3 两种注册方式的比较静态注册方式,由系统来管理receiver,而且程序里的所有receiver,可以在xml里面一目了然动态注册方式,隐藏在代码中,比较难发现;需要特别注意的是,在退出程序前要记得调用Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。三、 BroadcastReceiver生命周期一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效,当从该函数返回后,该对象就无效的了,结束生命周期。因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,应该在startService中来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。四、 BroadcastReceiver示例Activity[java] view plaincopyprint?public class PlayMusicRecevicer extends Activity implements OnClickListener { private Button playBtn; private Button stopBtn; private Button pauseBtn; private Button exitBtn; private Button closeBtn; private Intent intent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.music_receiver); playBtn = (Button) findViewById(R.id.play); stopBtn = (Button) findViewById(R.id.stop); pauseBtn = (Button) findViewById(R.id.pause); exitBtn = (Button) findViewById(R.id.exit); closeBtn = (Button) findViewById(R.id.close); playBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); pauseBtn.setOnClickListener(this); exitBtn.setOnClickListener(this); closeBtn.setOnClickListener(this); } @Override public void onClick(View v) { int op = -1; intent = new Intent("com.homer.receiver.musicReceiver"); switch (v.getId()) { case R.id.play: // play music op = 1; break; case R.id.stop: // stop music op = 2; break; case R.id.pause: // pause music op = 3; break; case R.id.close: // close activity this.finish(); break; case R.id.exit: // process by MusicReceiver op = 4; this.finish(); break; } Bundle bundle = new Bundle(); bundle.putInt("op", op); intent.putExtras(bundle); sendBroadcast(intent); // sendBroadcast } // private MusicReceiver receiver; // // @Override // protected void onStart(){ // super.onStart(); // // receiver = new MusicReceiver(); // IntentFilter filter = new IntentFilter(); // filter.addAction("com.homer.receiver.musicReceiver"); // this.registerReceiver(receiver, filter); // } // // @Override // protected void onStop(){ // this.unregisterReceiver(receiver); // // super.onStop(); // } @Override public void onDestroy(){ super.onDestroy(); if(intent != null){ stopService(intent); } } } BroadcastReceiver[java] view plaincopyprint?public class MusicReceiver extends BroadcastReceiver { // receive Broadcast @Override public void onReceive(Context context, Intent intent) { if(intent != null){ Bundle bundle = intent.getExtras(); Intent it = new Intent(context, MusicReceiverService.class); // call service for MusicReceiverService.class it.putExtras(bundle); if(bundle != null){ int op = bundle.getInt("op"); if(op == 4){ context.stopService(it); // stopService }else{ context.startService(it); // startService } } } } } Service(BroadcastReceiver调用的后台服务)[java] view plaincopyprint?public class MusicReceiverService extends Service { private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { Toast.makeText(this, "show media player", Toast.LENGTH_SHORT).show(); if (mediaPlayer == null) { mediaPlayer = MediaPlayer.create(this, R.raw.tmp); mediaPlayer.setLooping(false); } } @Override public void onDestroy() { Toast.makeText(this, "stop media player", Toast.LENGTH_SHORT); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } @Override public void onStart(Intent intent, int startId) { if (intent != null) { Bundle bundle = intent.getExtras(); if (bundle != null) { int op = bundle.getInt("op"); switch (op) { case 1: play(); break; case 2: stop(); break; case 3: pause(); break; } } } } public void play() { if (!mediaPlayer.isPlaying()) { mediaPlayer.start(); } } public void pause() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); } } public void stop() { if (mediaPlayer != null) { mediaPlayer.stop(); try { mediaPlayer.prepare(); // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数 } catch (IOException ex) { ex.printStackTrace(); } } } } AndroidManifest.xml[css] view plaincopyprint? 五、代码解析1、Activity中,PlayMusicService中通过重写OnClickListener 接口onClick()方法实现对播放音乐的控制,把音乐各种操作用数字通过Intent传递给service然后通过构造一个Intent , intent = new Intent("com.homer.receiver.musicReceiver"); 其中,com.homer.receiver.musicReceiver是 AndroidManifest.xml 对receiver的定义(或动态注册addAction为filter.addAction("com.homer.receiver.musicReceiver");)2、Activity中,音乐播放的控制,利用Bundle绑定数字op后,通过 sendBroadcast(intent); 广播出去Bundle bundle = new Bundle();bundle.putInt("op", op);intent.putExtras(bundle);startService(intent);3、 BroadcastReceiver中,会处理Activity启动的 sendBroadcast(intent); 广播,通过实现onReceive()方法,解析Activity中Intent的Bundle数据。然后通过Intent it = new Intent(context, MusicReceiverService.class); 初始化一个启动Service服务的Intent最后根据解析bundle的op数值决定启动context.startService(it); 服务 或 关闭context.stopService(it); 服务4、Service中,处理BroadcastReceiver广播启动的MusicReceiverService服务,即依次调用service的启动过程:onCreate --> onStart(可多次调用) --> onDestroyonCreate(), 创建mediaPlayeronStart(), 通过获取Bundle bundle = intent.getExtras();,提取int op = bundle.getInt("op");,然后执行响应的音乐播放操作onDestroy(),停止并释放mediaPlayer音乐资源,如果当执行context.stopService()时调用此方法5、Activity中,onClick()函数中close与exit是执行含义是不同的:close : 只是执行了this.finish(); 关闭了本Activity窗体,service并没有被关掉,音乐依然会继续在后台播放exit : 先调用了stopService(intent); 关闭了service服务,在Service中会调用3中的onDestroy()停止并释放音乐资源,后才执行this.finish(); 关闭了本Activity窗体六、BroadcastReceiver总结BroadcastReceiver需要先注册receriver(静态或动态)—> 发送广播sendBroadcast(intent) —> 处理广播onReceive(Context context, Intent intent) —> 启动服务startService(it) —> 关闭服务stopService(it) 其中,receriver两种注册方式,静态注册在AndroidManifest.xml中的receiver和动态注册在PlayMusicRecevicer注释的代码部分,两者选择一种即可