安卓组件-BroadcastReceiver
【转】http://emilyzhou.blog.51cto.com/3632647/685387
一、BroadcastReceiver的简介
用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似.
广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码.如:时区改变的通知,电池电量不足、用户改变了语言偏好或者开机启动等.
广播接收器没有用户界面,但是,它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.
二、BroadcastReceiver的两种注册方式
1)静态注册
静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action
首先,新建一个Android项目--->取名BroadcastReceiverDemo01
BroadcastActivity.java的代码
- public class BroadcastActivity extends Activity {
- private Button send = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.sned);
- send.setOnClickListener(new BroadcastListener());
- }
- class BroadcastListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- System.out.println("------------");
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_EDIT);
- BroadcastActivity.this.sendBroadcast(intent);
- }
- }
- }
TestReceiver.java的代码
- public class TestReceiver extends BroadcastReceiver {
- public TestReceiver() {
- System.out.println("TestReceiver create......");
- }
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- System.out.println("receive......");
- }
- }
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:id="@+id/sned"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TestReceiver!</string>
- <string name="app_name">broadcast</string>
- <string name="send">发送</string>
- </resources>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BroadcastActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!-- 注册Receiver -->
- <receiver android:name=".TestReceiver">
- <intent-filter>
- <action android:name="android.intent.action.EDIT"></action>
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
效果图
当用户点击发送的时候,程序会调用onReceive()方法 |
2)动态注册
动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action.
首先,新建一个Android项目--->取名BroadcastReceiverDemo02
BroadcastActivity.java的代码
- public class BroadcastActivity extends Activity {
- private Button send;
- private Button registerReceiver;
- private Button unregisterReceiver;
- private MyReceiver myReceiver;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.send);
- send.setOnClickListener(new SendButtontListener());
- registerReceiver = (Button)findViewById(R.id.registerReceiver);
- registerReceiver.setOnClickListener(new RegisterReceiverButtonListener());
- unregisterReceiver = (Button)findViewById(R.id.unregisterReceiver);
- unregisterReceiver.setOnClickListener(new UnregisterReceiverButtonListener());
- }
- class SendButtontListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("Intent.ACTION_EDIT");
- BroadcastActivity.this.sendBroadcast(intent);
- System.out.println("send-----");
- }
- }
- class RegisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- myReceiver = new MyReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("Intent.ACTION_EDIT");
- //动态注册BroadcastReceiver
- registerReceiver(myReceiver, filter);
- }
- }
- class UnregisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //注销BroadcastReceiver
- unregisterReceiver(myReceiver);
- System.out.println("close-----");
- }
- }
- }
MyReceiver.java的代码
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- System.out.println("onReceive......");
- }
- }
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:id="@+id/send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
- <Button
- android:id="@+id/registerReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/registerReceiver"
- />
- <Button
- android:id="@+id/unregisterReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/unregisterReceiver"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TestReceiver!</string>
- <string name="app_name">broadcast</string>
- <string name="send">发送广播</string>
- <string name="registerReceiver">注册广播接收器</string>
- <string name="unregisterReceiver">注销广播接收器</string>
- </resources>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BroadcastActivity"
- 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>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
效果图
当用户点击发送的时候,程序没有注册BroadcastReceiver,当用户点击注册广播接收器之后在点击发送会调用MyReceiver中的onReceive()方法,当用户点击注销广播接收器之后程序执行unregisterReceiver()方法 |
【转】http://blog.csdn.net/ithomer/article/details/7365147
一、 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
- <receiver android:name=".receiver.MusicReceiver" >
- <intent-filter>
- <action android:name="com.homer.receiver.musicReceiver" />
- </intent-filter>
- </receiver>
2 动态注册
Activity中,需在onStart()中调用registerReceiver()进行注册和在onStop中调用unregisterReceiver()释放服务
- 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
- 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
- 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调用的后台服务)
- 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
- <service
- android:name=".receiver.MusicReceiverService"
- android:enabled="true" >
- <intent-filter>
- <action android:name="com.homer.service.musicReceiverService" />
- </intent-filter>
- </service>
- <receiver android:name=".receiver.MusicReceiver" >
- <intent-filter>
- <action android:name="com.homer.receiver.musicReceiver" />
- </intent-filter>
- </receiver>
五、代码解析
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(可多次调用) --> onDestroy
onCreate(), 创建mediaPlayer
onStart(), 通过获取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注释的代码部分,两者选择一种即可
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。