Android中发送有序广播案例代码

Android中发送有序广播案例代码,第1张

概述Android系统提供了两种广播类型,一种是有序广播,一种是有序广播。(1)无序广播是完全异步执行,发送广播时所有监听这个广播的广播接收者都会收到此消息,但接收的顺序不确定。

AndroID系统提供了两种广播类型,一种是有序广播,一种是有序广播。

(1)无序广播是完全异步执行,发送广播时所有监听这个广播的广播接收者都会收到此消息,但接收的顺序不确定。

(2)有序广播是按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者中逻辑执行完毕后,才会继续传递。

实验要求:通过sendOrderedbroadeast()发送一条有序广播

1.在activity-main.xml布局文件中代码如下:

<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   xmlns:tools="http://schemas.androID.com/tools"   androID:ID="@+ID/activity_main"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:paddingBottom="@dimen/activity_vertical_margin"   androID:background="@drawable/stitch_one"   tools:context="cn.edu.bzu.orderedbroadcast.MainActivity">    <button     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_centerHorizontal="true"     androID:onClick="send"     androID:layout_margintop="50dp"     androID:text="发送有序广播"     androID:paddingleft="5dp"     androID:paddingRight="5dp"     androID:background="#FFD202"     androID:textSize="20sp"     /> </relativeLayout> 

2.在MainActivity中代码如下:

package cn.edu.bzu.orderedbroadcast;  import androID.content.Intent; import androID.support.v7.app.AppCompatActivity; import androID.os.Bundle; import androID.vIEw.VIEw;  public class MainActivity extends AppCompatActivity {    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);   }   public voID send(VIEw vIEw){     Intent intent=new Intent();     //定义广播事件类型     intent.setAction("Intercept_Stitch");     //发送广播     sendOrderedbroadcast(intent,null);   } } 

3.创建三个广播接收者

(1)MybroadcastReceiverOne

(2)MybroadcastReceiverTwo

(3)MybroadcastReceiverThree

(1)在MybroadcastReceiverOne中代码如下:

package cn.edu.bzu.orderedbroadcast;  import androID.content.broadcastReceiver; import androID.content.Context; import androID.content.Intent; import androID.util.Log;  public class MybroadcastReceiverOne extends broadcastReceiver {   public MybroadcastReceiverOne() {   }    @OverrIDe   public voID onReceive(Context context,Intent intent) {     Log.i("MybroadcastReceiverOne","自定义的广播接收者one,接收到了广播事件");   } } 

(2)在MybroadcastReceiverTwo中代码如下:

package cn.edu.bzu.orderedbroadcast;  import androID.content.broadcastReceiver; import androID.content.Context; import androID.content.Intent; import androID.util.Log;  public class MybroadcastReceiverTwo extends broadcastReceiver {   public MybroadcastReceiverTwo() {   }    @OverrIDe   public voID onReceive(Context context,Intent intent) {     Log.i("MybroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");   } } 

(3)在MybroadcastReceiverThree中代码如下:

package cn.edu.bzu.orderedbroadcast;  import androID.content.broadcastReceiver; import androID.content.Context; import androID.content.Intent; import androID.util.Log;  public class MybroadcastReceiverThree extends broadcastReceiver {   public MybroadcastReceiverThree() {   }    @OverrIDe   public voID onReceive(Context context,Intent intent) {     Log.i("MybroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");   } } 

4.在配置文件AndroIDManifest中代码如下:

<?xml version="1.0" enCoding="utf-8"?> <manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"   package="cn.edu.bzu.orderedbroadcast">    <application     androID:allowBackup="true"     androID:icon="@mipmap/ic_launcher"     androID:label="@string/app_name"     androID:supportsRtl="true"     androID:theme="@style/Apptheme">     <activity androID:name=".MainActivity">       <intent-filter>         <action androID:name="androID.intent.action.MAIN" />          <category androID:name="androID.intent.category.LAUNCHER" />       </intent-filter>     </activity>      <receiver     androID:name=".MybroadcastReceiverOne">     <intent-filter androID:priority="1000">       <action androID:name="Intercept_Stitch"/>     </intent-filter>     </receiver>     <receiver       androID:name=".MybroadcastReceiverTwo">       <intent-filter androID:priority="200">         <action androID:name="Intercept_Stitch"/>       </intent-filter>     </receiver>     <receiver       androID:name=".MybroadcastReceiverThree">       <intent-filter androID:priority="600">         <action androID:name="Intercept_Stitch"/>       </intent-filter>     </receiver>   </application>  </manifest> 

5.实验效果图:

运行程序后解决如下问题:

问题1:程序启动后,单击“发送有序广播”按钮,发出一条广播事件,此时观察LogCat窗口下的提示信息,输出什么?为什么?

点击按钮后出现如下图所示提示信息,原因是有序广播根据优先级接收广播信息的,在配置文件中广播接收者One的priority值最大,所以它先接收到广播,其次是Three,最后是Two。其中androID:priority="值"是用来设置广播接收者的优先级的,值越大,优先级别越高。

问题2:若将广播接收者MybroadcastReceiverTwo优先级同样设置为1000,并将MybroadcastReceiverTwo注册在MybroadcastReceiverOne前面,再来运行程序,观察结果,你能得出什么结论?

修改配置文件AndroIDManifest中代码如下:

<receiver   androID:name=".MybroadcastReceiverTwo">   <intent-filter androID:priority="1000">     <action androID:name="Intercept_Stitch"/>   </intent-filter> </receiver> <receiver androID:name=".MybroadcastReceiverOne">   <intent-filter androID:priority="1000">     <action androID:name="Intercept_Stitch"/> </intent-filter> </receiver>  <receiver   androID:name=".MybroadcastReceiverThree">   <intent-filter androID:priority="600">     <action androID:name="Intercept_Stitch"/>   </intent-filter> </receiver> 

运行程序出现如下图所示提示信息,原因是当广播接收者的优先级别相同时,先注册的广播接收者先接收到广播。

问题3:修改MybroadcastReceiverTwo如下,观察结果,你又可以得出什么结论?

public class MybroadcastReceiverTwo extends broadcastReceiver {   public MybroadcastReceiverTwo() {   }    @OverrIDe    public voID onReceive(Context context,"自定义的广播接收者Two,接收到了广播事件");     abortbroadcast();//有序广播拦截器     Log.i("MybroadcastReceiverTwo","我是广播接收者Two,广播被我终止了");   }  } 

运行程序出现如下图所示提示信息,原因是广播接收者Two的优先级最高,所以在Two中写一个拦截器,优先级低的广播接收者将接收不到广播信息,所以One和Three没有接收到广播事件,也就没有打印关于它们的信息。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android中发送有序广播案例代码全部内容,希望文章能够帮你解决Android中发送有序广播案例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存