发送有序广播

   在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.运行结果如下所示:


 

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值