赞
踩
有序广播:按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者的逻辑执行完毕后,才会继续传递。
abortBroadcast();终止广播
有序广播功能概述:
广播类型:
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn"
- android:text="发送有序广播"
- />
-
-
- </LinearLayout>

MainActivity.java
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
- MyReceiverOne one ;
- MyReceiverTow two;
- MyReceiverThree three;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- registerReceiver();
-
- Button button =findViewById(R.id.btn);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent=new Intent();
- intent.setAction("Intercept_Stitch");
- sendOrderedBroadcast(intent,null);
- }
- });
- }
-
- private void registerReceiver() {
- one=new MyReceiverOne();
- IntentFilter filter1=new IntentFilter();
- filter1.setPriority(1000);
- filter1.addAction("Intercept_Stitch");
- registerReceiver(one,filter1);
-
- two=new MyReceiverTow();
- IntentFilter filter2=new IntentFilter();
- filter2.setPriority(900);
- filter2.addAction("Intercept_Stitch");
- registerReceiver(two,filter2);
-
- three=new MyReceiverThree();
- IntentFilter filter3=new IntentFilter();
- filter3.setPriority(600);
- filter3.addAction("Intercept_Stitch");
- registerReceiver(three,filter3);
- }
-
-
-
- }

MyReceiverOne
- package com.example.myapplication;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- class MyReceiverOne extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("test","自定义的广播接收者One,接受到了广播事件");
- }
- }
MyRiverTwo
- package com.example.myapplication;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- class MyReceiverTow extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("test","自定义的广播接收者Two,接受到了广播事件");
- }
- }
MyReceiverThree
- package com.example.myapplication;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- class MyReceiverThree extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("test","自定义的广播接收者Three,接受到了广播事件");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。