相关文章推荐
俊秀的排球  ·  SqlServer ...·  1 年前    · 
有胆有识的单杠  ·  Java ...·  1 年前    · 
淡定的柠檬  ·  邮件 ...·  1 年前    · 
刀枪不入的青椒  ·  SQL ...·  1 年前    · 


1.无序广播,就普通广播,没有先后顺序,几乎同时收到消息

2.有序广播,可 以设置接收器的顺序优先级,还可以对广播设置设置截断,修改
通过设置 abortBroadcast()进行截断,通过setResultExtras(bundle)向下游广播接收器传递额外的键值对信息或者setResultData(“”)直接传送字符串 ,下游广播通过 getResultExtras 方法接收信息, getResultData ()方法接收字符串信息

示例有序广播
清单文件:
有序广播的顺序通过清单文件里的android:priority属性的数字大小进行优先级设置:范围为(-1000到1000)数字越大,优先级越高,

<intent-filter android:priority="003">

可参考下图:

android 优先接收到广播 android广播优先级的属性_android 优先接收到广播

<!--有序广播-->
        <receiver
            android:name=".broadcast.MyHaveBroadcastReceiver01"
            android:exported="true">
            <intent-filter>
                <action android:name="my_have_broadcast_receiver"></action>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".broadcast.MyHaveBroadcastReceiver02"
            android:exported="true">
            <intent-filter android:priority="003">
                <action android:name="my_have_broadcast_receiver"></action>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".broadcast.MyHaveBroadcastReceiver03"
            android:exported="true">
            <intent-filter android:priority="002">
                <action android:name="my_have_broadcast_receiver"></action>
            </intent-filter>
        </receiver>
/**
 * 有序广播01
public class MyHaveBroadcastReceiver01 extends BroadcastReceiver {
    private static final String TAG = "MyHaveBroadcastReceiver01";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            Bundle extras = intent.getExtras();
            String data = "";
            if (extras != null) {
                data = extras.getString("name");
            Log.d(TAG, "MyHaveBroadcastReceiver01 onReceive"+data);
}
/**
 * 有序广播02
public class MyHaveBroadcastReceiver02 extends BroadcastReceiver {
    private static final String TAG = "MyHaveBroadcastReceiver02";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            Bundle extras = intent.getExtras();
            String data = "";
            if (extras != null) {
                data = extras.getString("name3");
                 * 2.1有序广播可以通过setResultExtras向下游广播接收器传递数据
                extras.putString("name2", "喊你速速到龙阳路集合");
                setResultExtras(extras);
                 * 2.2有序广播可以通过setResultData向下游广播接收器传递字符串
                setResultData("2号口的信息");
            Log.d(TAG, "MyHaveBroadcastReceiver02 onReceive" + data);
             * 1.有序广播可以通过abortBroadcast();方法对广播进行截断
//            abortBroadcast();
}
/**
 * 有序广播03
public class MyHaveBroadcastReceiver03 extends BroadcastReceiver {
    private static final String TAG = "MyHaveBroadcastReceiver03";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            Bundle extras = intent.getExtras();
            String data = "";
            if (extras != null) {
                data = extras.getString("name");
            Log.d(TAG, "MyHaveBroadcastReceiver03 onReceive" + data);
             * 获取上游广播传递过来的数据
            //方法1
            Bundle bundle = getResultExtras(true);
            String strData = bundle.getString("name2");
            String resultData = getResultData();
            //方法2,直接获取字符串
            if (strData != null) {
                Log.d(TAG, "上游广播传递来的信息 onReceive" + strData +"\t\t\t"+ resultData);
}

Activity页面:StaticDynamicTestActivity.java

/**
 * 静态广播和动态广播的测试
 * 默认情况下,静态注册的广播隐式的收不到
 * 有序广播和无序广播的测试
public class StaticDynamicTestActivity extends AppCompatActivity {
    Button btnHaveOrder;
    Button btnNoHaveOrder;
    private MyDyBroadcastReceiver myDyBroadcastReceiver = new MyDyBroadcastReceiver();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static_dynamic_test);
        //注册动态广播接收器
        IntentFilter filter = new IntentFilter();
        filter.addAction("my_dy_broadcast_receiver");
        registerReceiver(myDyBroadcastReceiver, filter);
        registerReceiver(mBatteryBroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        btnHaveOrder = findViewById(R.id.btn_haveOrder);
        btnNoHaveOrder = findViewById(R.id.btn_noOrder);
        haveOrder();
        noOrder();
    //发送静态广播
    @SuppressWarnings("wrongConstant")
    public void sendStaticBroadcast(View view) {
        Intent intent = new Intent();
        intent.setAction("my_broadcast_receiver");
        //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
        //intent.addFlags(0x01000000);//不推荐
        intent.setPackage(getPackageName());
        sendBroadcast(intent);
    //发送动态广播
    @SuppressWarnings("wrongConstant")
    public void sendDyBroadcast(View view) {
        Intent intent = new Intent();
        intent.setAction("my_dy_broadcast_receiver");
//        //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
//        //intent.addFlags(0x01000000);//不推荐
//        intent.setPackage(getPackageName());
        sendBroadcast(intent);
    BroadcastReceiver mBatteryBroadcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int level = intent.getIntExtra("level", 0);
            Toast.makeText(context, "电池电量还有百分之" + level, Toast.LENGTH_LONG).show();
     * 有序广播
    void haveOrder() {
        btnHaveOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("my_have_broadcast_receiver");
                //这里注意传递个bundle参数
                Bundle bundle = new Bundle();
                bundle.putString("name3","回家吃饭了");
                intent.putExtras(bundle);
                //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
                intent.setPackage(getPackageName());
                sendOrderedBroadcast(intent, null);
     * 无序广播
    void noOrder() {
        btnNoHaveOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("my_no_broadcast_receiver");
                //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
                intent.setPackage(getPackageName());
                sendBroadcast(intent);
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除注册
        unregisterReceiver(myDyBroadcastReceiver);
        unregisterReceiver(mBatteryBroadcast);
}

Activity对应的xml页面:activity_static_dynamic_test.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".broadcast.StaticDynamicTestActivity">
    <Button
        android:id="@+id/bt_static"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendStaticBroadcast"
        android:text="发送静态广播啦">
    </Button>
    <Button
        android:id="@+id/btn_dy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="sendDyBroadcast"
        android:text="发送动态广播"
        app:layout_constraintTop_toBottomOf="@+id/bt_static" />
    <Button
        android:id="@+id/btn_battery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="sendDyBroadcast"
        android:text="获取电池电量"
        app:layout_constraintTop_toBottomOf="@+id/btn_dy">
    </Button>
    <Button
        android:id="@+id/btn_haveOrder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="sendDyBroadcast"
        android:text="有序广播"
        app:layout_constraintTop_toBottomOf="@+id/btn_battery">
    </Button>
    <Button
        android:id="@+id/btn_noOrder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="sendDyBroadcast"
        android:text="无序号广播"
        app:layout_constraintTop_toBottomOf="@+id/btn_haveOrder">
    </Button>
</androidx.constraintlayout.widget.ConstraintLayout>

微服务架构里security单独作为一个微服务怎么全局生效 spring security 微服务认证

从三板斧开始微服务下的SpringSecurity开始一、引入组件包<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency&g