当前位置:   article > 正文

有序广播 ,有序广播实例_有序广播的接收和发送 代码怎么写

有序广播的接收和发送 代码怎么写

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

abortBroadcast();终止广播

有序广播功能概述:

 广播类型:

 

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/btn"
  12. android:text="发送有序广播"
  13. />
  14. </LinearLayout>

MainActivity.java

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class MainActivity extends AppCompatActivity {
  9. MyReceiverOne one ;
  10. MyReceiverTow two;
  11. MyReceiverThree three;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. registerReceiver();
  17. Button button =findViewById(R.id.btn);
  18. button.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View view) {
  21. Intent intent=new Intent();
  22. intent.setAction("Intercept_Stitch");
  23. sendOrderedBroadcast(intent,null);
  24. }
  25. });
  26. }
  27. private void registerReceiver() {
  28. one=new MyReceiverOne();
  29. IntentFilter filter1=new IntentFilter();
  30. filter1.setPriority(1000);
  31. filter1.addAction("Intercept_Stitch");
  32. registerReceiver(one,filter1);
  33. two=new MyReceiverTow();
  34. IntentFilter filter2=new IntentFilter();
  35. filter2.setPriority(900);
  36. filter2.addAction("Intercept_Stitch");
  37. registerReceiver(two,filter2);
  38. three=new MyReceiverThree();
  39. IntentFilter filter3=new IntentFilter();
  40. filter3.setPriority(600);
  41. filter3.addAction("Intercept_Stitch");
  42. registerReceiver(three,filter3);
  43. }
  44. }
MyReceiverOne
  1. package com.example.myapplication;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. class MyReceiverOne extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("test","自定义的广播接收者One,接受到了广播事件");
  10. }
  11. }
MyRiverTwo
  1. package com.example.myapplication;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. class MyReceiverTow extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("test","自定义的广播接收者Two,接受到了广播事件");
  10. }
  11. }
MyReceiverThree
  1. package com.example.myapplication;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. class MyReceiverThree extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("test","自定义的广播接收者Three,接受到了广播事件");
  10. }
  11. }

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/902092
推荐阅读
相关标签