Android -----有序广播

  广播分为有序广播和无序广播,本次主要讲有序广播。

有序广播:

一,优缺点

优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver

2,通过abortBroadcast可终止广播的传播

缺点:效率低

二,发送广播的方法:sendOrderedBroadcast()

三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,

下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据


1、界面代码如下:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
       tools:context="com.example.bz0209.bryouxu.MainActivity"
       android:background="@drawable/stitch_one">
    
    <Button
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送有序广播"
        android:textSize="18sp" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="80dp"
      android:background="#FBFBFF"
        android:paddingLeft="5dp"
       android:paddingRight="5dp"
        android:onClick="send"/>


  </RelativeLayout>
 
2、MainActivity代码:

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、MyBroadcastReceiverOne:
  
  
public class MyBroadcastReceiverOne extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverOne","自定义的广播接受者One,接受到广播事件");
    }
}
4、MyBroadcastReciverTwo:
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceivertwo","自定义的广播接受者two,接受到广播事件");
    }
}
5、MyBroadcastReceiverThree:
public class MyBroadcastReceiverThree extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverThree","自定义的广播接受者Therr,接受到广播事件");
    }
}


 
 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值