android四大组件-----广播 自定义广播,优先级的使用

mainifest.xml

 <!--设置广播的级别-->
        <receiver android:name=".MyReceiver">
            <intent-filter
                android:priority="1000"
                >
                <action android:name="com.yifei.myapplication.myReceiver"/>
            </intent-filter>
        </receiver>

        <receiver android:name=".ReceiverText">
            <intent-filter
                android:priority="500"
                >
                <action android:name="com.yifei.myapplication.myReceiver"/>
            </intent-filter>
        </receiver>

acitivity

package com.yifei.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button btn;
public static String TAG ="MainActivity123";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn= findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.yifei.myapplication.myReceiver");
                sendOrderedBroadcast(intent,null);
            }
        });
    }
}

两个广播


public class ReceiverText extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(MainActivity.TAG, "onReceive: 测试广播");
    }
}
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(MainActivity.TAG, "onClick: 发送一条有序广播");
        //截断广播
//        abortBroadcast();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41069726/article/details/89786218
今日推荐