Android--广播的研究
Android中的广播大致有两种:有序广播和标准广播。标准广播是一种完全异步执行的广播,广播发出后,广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序而言,这种广播的执行效率较高,但是也以为这它是无法被截断的。有序广播是一种同步执行的广播,广播发出后,同一时刻只会有一个广播接收器接收到广播,当这个广播接收器中的逻辑执行完毕后才会继续传播消息。并且前面的广播可以截断正在传播的广播。
下面看一个标准广播的例子:
1.先建立一个接收类,要继承BroadcastReciver类:
package org.lxh.demo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); } }
2.配置AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Hello" 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 android:name="org.lxh.demo.MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver> </application> </manifest>
即:
<receiver android:name="org.lxh.demo.MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver>
3。发送程序:
package org.lxh.demo; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Hello extends Activity { private Button btn=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 requestWindowFeature(Window.FEATURE_NO_TITLE); super.setContentView(R.layout.main); // 设置要使用的布局管理器 this.btn=(Button)super.findViewById(R.id.mybtn); this.btn.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener{ public void onClick(View arg0) { Intent intent=new Intent("com.example.broadcast.MY_BROADCASTTEST"); sendBroadcast(intent); } } }
运行如下:
下面我们再建立一个广播接收类:
package org.lxh.demo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AnotherBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show(); } }
再配置一次AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Hello" 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 android:name="org.lxh.demo.MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver> <receiver android:name="org.lxh.demo.AnotherBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver> </application> </manifest>
两个广播接收器过滤同一个广播,所以他们会同时收到此广播,运行如下:
即先显示第一个注册的广播,后显示第二个。我们考虑如何改变接收的顺序?可以设置优先级<intent-filter android:priority="100">:
<receiver android:name="org.lxh.demo.MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver> <receiver android:name="org.lxh.demo.AnotherBroadcastReceiver"> <intent-filter android:priority="100"> <action android:name="com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter> </receiver>
此时运行接收顺序就反过来了。下面考虑如何不让第二个接收器接收到广播?我们可以再第一个接收器内写入abortBroadcast();即:
package org.lxh.demo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AnotherBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show(); abortBroadcast(); } }
此时运行会报错:
04-22 01:48:58.187: E/BroadcastReceiver(435): BroadcastReceiver trying to return result during a non-ordered broadcast
04-22 01:48:58.187: E/BroadcastReceiver(435): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:451)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.content.BroadcastReceiver.abortBroadcast(BroadcastReceiver.java:374)
04-22 01:48:58.187: E/BroadcastReceiver(435): at org.lxh.demo.AnotherBroadcastReceiver.onReceive(AnotherBroadcastReceiver.java:14)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.os.Looper.loop(Looper.java:123)
04-22 01:48:58.187: E/BroadcastReceiver(435): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-22 01:48:58.187: E/BroadcastReceiver(435): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 01:48:58.187: E/BroadcastReceiver(435): at java.lang.reflect.Method.invoke(Method.java:507)
04-22 01:48:58.187: E/BroadcastReceiver(435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-22 01:48:58.187: E/BroadcastReceiver(435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-22 01:48:58.187: E/BroadcastReceiver(435): at dalvik.system.NativeStart.main(Native Method)
提示这是一个没有顺序的广播。怎么改正呢?只需将发送广播的程序改为sendOrderedBroadcast(intent, null);:
package org.lxh.demo; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Hello extends Activity { private Button btn=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 requestWindowFeature(Window.FEATURE_NO_TITLE); super.setContentView(R.layout.main); // 设置要使用的布局管理器 this.btn=(Button)super.findViewById(R.id.mybtn); this.btn.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener{ public void onClick(View arg0) { Intent intent=new Intent("com.example.broadcast.MY_BROADCASTTEST"); //sendBroadcast(intent); sendOrderedBroadcast(intent, null); } } }
此时再运行实例,只会显示第一条Toast:
并且不会报错。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。