BroadcastReceiver的优先级

BroadcastReceiver的优先级是可以设置的:如下:

 <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="9">
                <action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver1"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="10">
                <action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" />
            </intent-filter>
        </receiver>

  除了可以设置优先级外,还可以设置优先级高的broadcastReceiver执行完后,中断优先级低的,让优先级不能获取执行权限:

package com.example.yabushan.broadercastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//第一个broadcastReceiver
public class MyReceiver extends BroadcastReceiver {
   // public  final String ACTION="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver";
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
       System.out.println("接收到广播数据:"+intent.getStringExtra("DATA").toString());
    }
}

  

package com.example.yabushan.broadercastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//第二个BroadercastReceiver
public class MyReceiver1 extends BroadcastReceiver {
    public MyReceiver1() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
       System.out.println("MyReceiverl 接收到了信息。");
//在androidmanifest.xml中设置该broadcastreceiver的优先级高 abortBroadcast(); } }

  

package com.example.yabushan.broadercastreceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.deleverData).setOnClickListener(this);
        findViewById(R.id.reg).setOnClickListener(this);
        findViewById(R.id.unreg).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.deleverData:
                //这里需要使用隐示Intent
               // Intent  intent=new Intent(this,MyReceiver.class);
              //  System.out.println(myReceiver.ACTION);
                Intent intent=new Intent("com.example.yabushan.broadercastreceiver.intent.action.MyReceiver");
                intent.putExtra("DATA","您好");
               // sendBroadcast(intent);
          //这里需要用排序的发送方式 sendOrderedBroadcast(intent,null); break; case R.id.reg:
//注册 if(myReceiver==null){ myReceiver =new MyReceiver(); registerReceiver(myReceiver,new IntentFilter(myReceiver.ACTION)); } break; case R.id.unreg: if(myReceiver!=null){ //解除注册 unregisterReceiver(myReceiver); } break; } } private MyReceiver myReceiver=null; }

  使用相同的action!<?xml version="1.0" encoding="utf-<manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.example.yabushan.broadercastreceiver" >


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="9">
<action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver1" android:enabled="true" android:exported="true" > <intent-filter android:priority="10"> <action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" /> </intent-filter> </receiver> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="传递消息"
        android:id="@+id/deleverData"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/reg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注销"
        android:id="@+id/unreg" />
</LinearLayout>

 

转载于:https://www.cnblogs.com/yabushan/p/4993101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值