AS中Android8版本静态注册和动态注册的相关结论和知识

静态注册
创建:
在这里插入图片描述
具体代码如下

package com.example.myrc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver4 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

广播接收者创建后,as工具会自动在AndrManifest.xml中注册广播接收者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myrc">

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

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

但是在Android8.0之后使用静态广播接收者将无法接收到广播,但可以用setComponent方法可以接收
intent.setComponent(new ComponentName(“com.example.myrc”,“com.example.myrc.MyReceiver”));后面俩参数是带包名的路径
我创建了3个广播接收者并设置了优先级 android:priority="" priority数值越大优先级越高

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myrc">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".MyReceiver4"
            android:enabled="true"
            android:exported="true"></receiver>
        <!-- 顺序是2 1 3 -->
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="500">
                <action android:name="Help" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="800">
                <action android:name="Help" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver3"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="300">
                <action android:name="Help" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

主函数

package com.example.myrc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
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 {
    public MyReceiver one;
    public MyReceiver2 two;
    public MyReceiver3 three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }
    public void send(View view){
        Intent intent=new Intent();
        intent.setAction("Help");
      intent.setComponent(new ComponentName("com.example.myrc","com.example.myrc.MyReceiver"));
        intent.setComponent(new ComponentName("com.example.myrc","com.example.myrc.MyReceiver2"));
     intent.setComponent(new ComponentName("com.example.myrc","com.example.myrc.MyReceiver3"));
//        intent.setPackage("com.example.myrc");

        sendOrderedBroadcast(intent,null);
       
    }


}

MyReceiver2.java

package com.example.myrc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("main","广播接收者2,接收到了广播事件");

    }
}

其他几个和这类似
结果:
在这里插入图片描述从这可以发现setComponent具有覆盖性
这里如果想接受三个广播的话可以用
intent.setPackage(“com.example.myrc”);
结果附图:在这里插入图片描述
需要注意的是setComponent和setPackage一起用前者的优先级高

2动态注册
它在函数里面注册不需要在xml文件中去定义
注册代码

  one=new MyReceiver();
       IntentFilter filter1=new IntentFilter();
       filter1.setPriority(100);
       filter1.addAction("Help");
      registerReceiver(one,filter1);
        two=new MyReceiver2();
        IntentFilter filter2=new IntentFilter();
        filter2.setPriority(100);
        filter2.addAction("Help");
        registerReceiver(two,filter2);
        three=new MyReceiver3();
        IntentFilter filter3=new IntentFilter();
        filter3.setPriority(100);
        filter3.addAction("Help");
        registerReceiver(three,filter3);

我这个事例演示了动态注注册接收者的优先级并且一个接收者接收俩种广播并带回数据代码如下:
MainActivity.java

package com.example.myrc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
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 {
    public MyReceiver one;
    public MyReceiver2 two;
    public MyReceiver3 three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    //动态注册  注册顺序为3 1 2
    public void init(){
       one=new MyReceiver();
       IntentFilter filter1=new IntentFilter();
       filter1.setPriority(100);
       filter1.addAction("Help");
      registerReceiver(one,filter1);
        two=new MyReceiver2();
        IntentFilter filter2=new IntentFilter();
        filter2.setPriority(100);
        filter2.addAction("Help");
        registerReceiver(two,filter2);
        three=new MyReceiver3();
        IntentFilter filter3=new IntentFilter();
        filter3.setPriority(100);
        filter3.addAction("Help");
        registerReceiver(three,filter3);
//注册广播接收者1注册广播Help1
        IntentFilter filter4=new IntentFilter();
        filter4.setPriority(100);
        filter4.addAction("Help1");
        registerReceiver(one,filter4);

    }
    public void send(View view){
        Intent intent=new Intent();
        intent.setAction("Help");


        sendOrderedBroadcast(intent,null);
       );
        Intent intent1=new Intent();
        intent1.setAction("Help1");
        intent1.putExtra("name","刘德华");//通过putExtra设置数据
        intent1.putExtra("age",40);
        sendOrderedBroadcast(intent1,null);
    }
}

MyReceiver接收俩个广播代码如下

package com.example.myrc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("main","广播接收者1,接收到了广播事件");
        String action=intent.getAction();//通过getAction()方法获取广播名
        if(action.equals("Help")){
            Log.i("main","处理广播Help");
        }
        else if(action.equals("Help1")){
            Log.i("main","处理广播Help1");
            Log.i("main","name="+intent.getStringExtra("name"));//通过getExtra()获取广播带过来的数据
            Log.i("main","age="+intent.getIntExtra("age",0));
        }
    }
}


其他接收者代码

package com.example.myrc;

        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.util.Log;

public class MyReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("main","广播接收者2,接收到了广播事件");

    }
}

结果如下:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值