静态注册
创建:
具体代码如下
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,接收到了广播事件");
}
}
结果如下: