发送有序广播

来源:互联网 发布:域名在哪续费 编辑:程序博客网 时间:2024/06/14 05:03

   在Android系统中,提供两种广播类型,有序广播和无序广播。

以下程序是根据有序广播编写的发送有序广播。

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

  有序广播的特点,如下图所示:

    

  

 

 1.  编写用户交互界面

   a. 代码如下所示:

<?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:background="@drawable/stitch_one"    tools:context="com.example.bz0209.youxu.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="80dp"        android:onClick="send"        android:text="发送有序广播"        android:paddingLeft="5dp"        android:paddingRight="5dp"        android:background="#FBFBFF"        android:textSize="20sp"        /></RelativeLayout>b.结果如图所示: c.点击send,发送广播按钮处理事件,代码如下:
package com.example.bz0209.youxu;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");        sendBroadcast(intent,null);    }}
2.创建广播接收者 MyReceiverOne:
package com.example.bz0209.youxu;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverOne extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.      Log.i("MyReceiverOne","自定义的广播接受者One,接收到了广播事件");    }} 依次创建广播接收者MyReceiverTwo,MyReceiverThree,代码如下:
a.MyReceiverTwo
package com.example.bz0209.youxu;
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverTwo extends BroadcastReceiver {    public MyReceiverTwo() {    }    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        //throw new UnsupportedOperationException("Not yet implemented");        Log.i("MyReceiverTwo","自定义的广播接受者Two,接收到了广播事件");    }}
b.MyReceiverThree
package com.example.bz0209.youxu;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverThree extends BroadcastReceiver {    public MyReceiverThree() {    }    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        //throw new UnsupportedOperationException("Not yet implemented");        Log.i("MyReceiverThree","自定义的广播接受者Three,接收到了广播事件");    }}

3.注册广播接收者,设置广播接收者的优先级:  <receiver
    android:name=".MyReceiverOne"    android:enabled="true"    android:exported="true">    <intent-filter android:priority="1000">        <action android:name="Intercept_Stitch" />    </intent-filter></receiver>  <receiver    android:name=".MyReceiverTwo"    android:enabled="true"    android:exported="true">    <intent-filter android:priority="800">        <action android:name="Intercept_Stitch" />    </intent-filter></receiver>  <receiver    android:name=".MyReceiverThree"    android:enabled="true"    android:exported="true">    <intent-filter android:priority="500">        <action android:name="Intercept_Stitch" />    </intent-filter></receiver>
通过属性priority数值设置优先级,数值大的,先接收广播。因此,MyReceiverOne
先接收,依次为MyReceiverTwo,MyReceiverThree.

4.运行结果如下所示:


 

 


原创粉丝点击