每天记录学习的新知识 : 有序广播 sendOrderedBroadcast

简介:

有序广播,
从优先级别最高的广播接收器开始接收,接收完了如果没有丢弃,就下传给下一个次高优先级别的广播接收器进行处理,
依次类推,直到最后。
如果多个应用程序设置的优先级别相同,执行顺序不确定,如果没有设置优先级,默认是0。

API:

sendOrderedBroadcast(intent,null);  

作用是:发送Ordered Broadcast

sendOrderedBroadcast(intent, receiverPermission, resultReceiver, 
						scheduler, initialCode, initialData, initialExtras);

作用是:发送Ordered Broadcast

参数 resultReceiver,可以自己重写一个类,作为一个最终的receive 最后都能够接收到广播,最终的receiver 不需要再清单文件里面配置,
参数 initialData可以作为传输的数据。

静态注册
android:priority="10"

动态注册
intentFilter.setPriority(100);

作用是:设置优先级,优先级越高的越早能接收到广播。(取值范围:-1000~10000)

setResultExtras(Bundle)

作用是:将数据存入Broadcast , 再传递给下一级接收者。

Bundle bundle = getResultExtras(true)

作用是:获取上一级传递过来的数据。(上上一级也阔以)

abortBroadcast()

作用是:中断广播,不会再响比它有优先级低得广播再传播下去了

事例:

public class SendOrderBroadcastActivity extends Activity {

    static final String TAG = "SendOrderBroadcastActivity";
    static final String ACTION = "com.x.text.SendOrderBroadcastActivity";
    static final String KEY = "com.x.text.SendOrderBroadcastActivity";
    static final String KEY_MAIN = "com.x.text.main";
    @BindView(R.id.order_btn)
    TextView orderBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_broadcast);
        ButterKnife.bind(this);
        initReceiver1();
        initReceiver2();
        initReceiver3();
    }
    EndReceiver myReceiver;
    private void initReceiver3() {
         myReceiver = new EndReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        registerReceiver(myReceiver, intentFilter);
    }

    private void initReceiver2() {
        MyReceiver2 myReceiver = new MyReceiver2();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(-200);
        intentFilter.addAction(ACTION);
        registerReceiver(myReceiver, intentFilter);
    }

    private void initReceiver1() {
        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(200);
        intentFilter.addAction(ACTION);
        registerReceiver(myReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.order_btn)
    public void onViewClicked() {
        Intent intent = new Intent(ACTION);
        intent.putExtra(KEY_MAIN,"存入数值KEY_MAIN");
        sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        Log.i(TAG,"最后的广播");
                    }
                }, null,
                0, null, null);
    }

    private class EndReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = getResultExtras(true);
            String name = bundle.getString(KEY);
            Log.i(TAG, "EndReceiver onReceive:"+name);
        }
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "MyReceiver onReceive");
            Bundle bundle = new Bundle();
            bundle.putString(KEY,"优先级高的广播存入数值");
            setResultExtras(bundle);
        }
    }

    public class MyReceiver2 extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = getResultExtras(true);
            String name = bundle.getString(KEY);
            Log.i(TAG, "MyReceiver2 onReceive:"+name);
        }
    }
}
Log输出:
MyReceiver onReceive
EndReceiver onReceive:优先级高的广播存入数值
MyReceiver2 onReceive:优先级高的广播存入数值
最后的广播

参考:
Android学习笔记(十二)BroadcastReceiver的有序广播和优先级:
https://www.cnblogs.com/codingblock/p/4856559.html

你可能感兴趣的:(Android,四大组件/碎片)