今天我们来学习一下BroadReceiver这个组件。我们首先来粗略地认识一下BroadReceiver这个组件。也就是说,如果有谁向这个组件发送消息的话,那么这个消息就会被接受到。注册BroadReceiver有俩种方法,一种是静态的注册。也就是BroadReceiver必须在AndroidManifest.xml中进行注册。但是我们也可以使用动态注册的方法,所谓的动态注册就是在AndroidManifest.xml中不需要注册BroadReceiver这个组件。我们只需要在BroadCastReceiver的一个类中写明一个ACTION就好了。这是要注意的。比如就像下面一样就可以进行动态的注册了。package startservicefromanotherapp.lg.com.broadcastapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/*我们在写ACTION的时候,其实也是有一定的规则的,不是乱写的。包名+intent.action+类名*/
public class MyReceiver extends BroadcastReceiver {
public static String ACTION = "startservicefromanotherapp.lg.com.broadcastapp.Intent.action.MyReceiver";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
System.out.println("MyReceiver接受到了消息" + intent.getStringExtra("data"));
// 阻止广播再往优先级低的广播接收器进行传播
abortBroadcast();
}
}
下面要进行的是就是说明如何进行动态的注册:
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myReceiver == null) {
myReceiver=new MyReceiver();
registerReceiver(myReceiver, new IntentFilter(MyReceiver.ACTION));
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myReceiver != null) {
unregisterReceiver(myReceiver);
/*在注销的时候,那么我们也应该要注意的是需要将myReceiver置为null,
* 否则的话,程序就不会正常的执行*/
myReceiver=null;
}
}
});
当然,我们注册完成以后,如果要向这个广播中发送消息的话,那么发送的方式和静态的时候,又是不一样的。
btnSendMsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这种方式是通过静态的方式来进行的。如果我们是使用动态的方式来进行的话,那么
//我们就不能使用这种方式来发送数据了
/* Intent i = new Intent(MainActivity.this, MyReceiver.class);
i.putExtra("data", "发送数据信息");
sendBroadcast(i);*/
Intent intent = new Intent(MyReceiver.ACTION);
intent.putExtra("data", "hello");
//sendBroadcast(intent);
// 如果是要阻止广播向优先级低的广播进行传播的话,那么就需要使用有序广播
// 也就是需要使用 sendOrderedBroadcast(intent, null);后面的一个参数一般情况下,
// 都是设置为null的。
//
sendOrderedBroadcast(intent, null);
}
});
如果是静态的的话,那么注册监听器和发送广播都是比较简单的。
Intent i = new Intent(MainActivity.this, MyReceiver.class);
i.putExtra("data", "发送数据信息");
sendBroadcast(i)
广播也是有优先级的。那么如果要使优先级高的广播接收到消息后,要阻止消息向优先级低的广播进行传播的话,应该怎么做?先看一下AndroidManifest.xml中的内容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="startservicefromanotherapp.lg.com.broadcastapp">
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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="10">
<action android:name="startservicefromanotherapp.lg.com.broadcastapp.Intent.action.MyReceiver" />
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver1"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="8">
<action android:name="startservicefromanotherapp.lg.com.broadcastapp.Intent.action.MyReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
那么首先在发送广播的时候,必须使用有序广播的方式,并且在优先级较高的广播接收到消息以后,就必须使用阻止消息的传播,具体的代码如下:
//有序广播
Intent intent = new Intent(MyReceiver.ACTION);
intent.putExtra("data", "hello");
//sendBroadcast(intent);
// 如果是要阻止广播向优先级低的广播进行传播的话,那么就需要使用有序广播
// 也就是需要使用 sendOrderedBroadcast(intent, null);后面的一个参数一般情况下,
// 都是设置为null的。
//
sendOrderedBroadcast(intent, null);
//阻止消息向优先级低的广播传播:
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
System.out.println("MyReceiver接受到了消息" + intent.getStringExtra("data"));
// 阻止广播再往优先级低的广播接收器进行传播
abortBroadcast();
}
以上就是关于BroadCastReceiver的全部的内容。