您的位置:首页 > 移动开发 > Android开发

Android学习之四大组件之广播接收器BroadcastReceiver

2016-02-28 10:41 555 查看
BroadcastReceiver顾名思义也就是说"广播接收器"的意思,它用来接收系统和应用中的广播。作为四大组件之一想必大家肯定比我更熟悉。

BroadcastReceiver的注册方式有2种:

1.静态注册:

静态注册是在AndroidManifest.xml中配置的,例如

<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="com.yuzhentao.staticaction" />
</intent-filter>
</receiver>


之后只要是

"com.yuzhentao.staticaction"


这个地址的广播,StaticReceiver都能收到,StaticReceiver代码如下

/**
* 静态广播的BroadcastReceiver
*
* @author yuzhentao
*/
public class StaticReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_STATIC)) {
SharedPreferences.Editor et = context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE).edit();
et.putString(Constants.SP_KEY_STATIC, intent.getStringExtra(Constants.MSG_STATIC));
et.apply();
}
}

}

这里通过SharedPreferences将收到的消息存储起来,同时我将一些常用的常量提取出来写在了一个类里面,方便使用,个人习惯呵呵。常量类如下

/**
* 常量
*
* @author yuzhentao
*/
public class Constants {

public static final String ACTION_STATIC = "com.yuzhentao.staticaction";
public static final String ACTION_DYNAMIC = "com.yuzhentao.dynamicaction";
public static final String ACTION_DISORDERLY= "com.yuzhentao.disorderlyaction";
public static final String ACTION_ORDERLY= "com.yuzhentao.orderlyaction";
public static final String MSG_STATIC = "msg_static";
public static final String MSG_DYNAMIC = "msg_dynamic";
public static final String MSG_DISORDERLY = "msg_disorderly";
public static final String MSG_ORDERLY = "msg_orderly";
public static final String SP_NAME = "sp_name";
public static final String SP_KEY_STATIC = "sp_key_static";

}


这样MainActivity中就能获取到收到的广播,MainActivity中肯定是要做发送广播的操作。我们可以设置通过点击Button的方式来发送广播。

case R.id.button_static_activity_main:
Intent intentStatic = new Intent(Constants.ACTION_STATIC);
intentStatic.putExtra(Constants.MSG_STATIC, "收到静态广播");
sendBroadcast(intentStatic);
sendStaticMsg();
break;

这里有个sendStaticMsg(),通过异步的方式来显示文本也就是说收到的消息。

/**
* 发送静态广播的消息
*/
private void sendStaticMsg() {
handlerStatic.post(new StaticRunnable());
}

/**
* 静态广播的Runnable
*/
private class StaticRunnable implements Runnable {

@Override
public void run() {
tvStatic.setText(sp.getString(Constants.SP_KEY_STATIC, ""));
}

}


2.动态注册:
动态注册需要在代码中指定广播并注册和注销,先在MainActivity中声明一个BroadcastReceiver,如下

private BroadcastReceiver broadcastReceiverStatic = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_DYNAMIC)) {
tvDynamic.setText(intent.getStringExtra(Constants.MSG_DYNAMIC));
}
}
};

在onCreate()中注册广播,MainActivity创建广播就注册

IntentFilter intentFilter = new IntentFilter(Constants.ACTION_DYNAMIC);
registerReceiver(broadcastReceiverStatic, intentFilter);

在onDestroy()中注销广播,MainActivity销毁广播就注销

unregisterReceiver(broadcastReceiverStatic);

点击Button来发送广播

case R.id.button_dynamic_activity_main:
Intent intentDynamic = new Intent(Constants.ACTION_DYNAMIC);
intentDynamic.putExtra(Constants.MSG_DYNAMIC, "收到动态广播");
sendBroadcast(intentDynamic);
break;

之后声明的BroadcastReceiver的onReceiver()中就会收到广播。

BroadcastReceiver有2种:

1.无序广播:

无序广播就是普通的广播,不需要声明优先级,这里用静态注册的方式来演示下无序广播的效果,先创建3个BroadcastReceiver,如下

/**
* 无序广播的BroadcastReceiver1
*
* @author yuzhentao
*/
public class DisorderlyReceiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_DISORDERLY)) {
String msg = intent.getStringExtra(Constants.MSG_DISORDERLY);
Log.e("DisorderlyReceiver1", msg);
}
}

}

/**
* 无序广播的BroadcastReceiver2
*
* @author yuzhentao
*/
public class DisorderlyReceiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_DISORDERLY)) {
String msg = intent.getStringExtra(Constants.MSG_DISORDERLY);
Log.e("DisorderlyReceiver2", msg);
}
}

}


/**
* 无序广播的BroadcastReceiver3
*
* @author yuzhentao
*/
public class DisorderlyReceiver3 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_DISORDERLY)) {
String msg = intent.getStringExtra(Constants.MSG_DISORDERLY);
Log.e("DisorderlyReceiver3", msg);
}
}

}

别忘记在AndroidManifest.xml中配置

<receiver android:name=".DisorderlyReceiver1">
<intent-filter>
<action android:name="com.yuzhentao.disorderlyaction" />
</intent-filter>
</receiver>
<receiver android:name=".DisorderlyReceiver2">
<intent-filter>
<action android:name="com.yuzhentao.disorderlyaction" />
</intent-filter>
</receiver>
<receiver android:name=".DisorderlyReceiver3">
<intent-filter>
<action android:name="com.yuzhentao.disorderlyaction" />
</intent-filter>
</receiver>

然后点击按钮发送广播

case R.id.button_disorderly_activity_main:
Intent intentDisorderly = new Intent(Constants.ACTION_DISORDERLY);
intentDisorderly.putExtra(Constants.MSG_DISORDERLY, "收到无序广播");
sendBroadcast(intentDisorderly);
break;

然后运行,打印Log

02-28 11:09:04.015 13861-13861/com.yuzhentao.broadcastreceiverdemo E/DisorderlyReceiver1:
收到无序广播

02-28 11:09:04.017 13861-13861/com.yuzhentao.broadcastreceiverdemo E/DisorderlyReceiver2: 收到无序广播

02-28 11:09:04.018 13861-13861/com.yuzhentao.broadcastreceiverdemo E/DisorderlyReceiver3: 收到无序广播

无序广播对于多个接收者来说是完全异步的,通常每个接收者都无需等待即可以接收到广播,接收者相互之间不会有影响。对于这种广播,接收者无法终止广播,即无法阻止其他接收者的接收动作。也就是说我们在onReceiver()即使加上

abortBroadcast();

也是没有卵用的

2.有序广播:

同样采用静态注册,先创建3个BroadcastReceiver,如下

/**
* 有序广播的BroadcastReceiver1
*
* @author yuzhentao
*/
public class OrderlyReceiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_ORDERLY)) {
String msg = intent.getStringExtra(Constants.MSG_ORDERLY);
Log.e("OrderlyReceiver1", msg);
Bundle bundle = new Bundle();
bundle.putString(Constants.MSG_ORDERLY, msg + "@OrderlyReceiver1");
setResultExtras(bundle);
//            abortBroadcast();
}
}

}

/**
* 有序广播的BroadcastReceiver2
*
* @author yuzhentao
*/
public class OrderlyReceiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_ORDERLY)) {
String msg = getResultExtras(true).getString(Constants.MSG_ORDERLY);
Log.e("OrderlyReceiver2", msg);
Bundle bundle = new Bundle();
bundle.putString(Constants.MSG_ORDERLY, msg + "@OrderlyReceiver2");
setResultExtras(bundle);
}
}

}

/**
* 有序广播的BroadcastReceiver3
*
* @author yuzhentao
*/
public class OrderlyReceiver3 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_ORDERLY)) {
String msg = getResultExtras(true).getString(Constants.MSG_ORDERLY);
Log.e("OrderlyReceiver3", msg);
}
}

}

这里使用setResultExtras()来加工下收到的消息,为了方便演示。不同的地方在于AndroidManifest.xml中的配置,使用sendOrderedBroadcast()方法发送有序广播时,需要一个权限参数,如果为null则表示不要求接收者声明指定的权限,如果不为null,则表示接收者若要接收此广播,需声明指定权限。这样做是从安全角度考虑的,例如系统的短信就是有序广播的形式,一个应用可能是具有拦截垃圾短信的功能,当短信到来时它可以先接受到短信广播,必要时终止广播传递,这样的软件就必须声明接收短信的权限。

所以我们在AndroidMainfest.xml中定义一个权限:

<permission
android:name="yuzhentao.permission.MY_BROADCAST_PERMISSION"
android:protectionLevel="normal" />
<uses-permission android:name="yuzhentao.permission.MY_BROADCAST_PERMISSION" />

然后广播注册时当然也要加上优先级了,优先级范围为-1000到1000,数值越大优先级越高

<receiver android:name=".OrderlyReceiver1">
<intent-filter android:priority="1000">
<action android:name="com.yuzhentao.orderlyaction" />
</intent-filter>
</receiver>
<receiver android:name=".OrderlyReceiver2">
<intent-filter android:priority="999">
<action android:name="com.yuzhentao.orderlyaction" />
</intent-filter>
</receiver>
<receiver android:name=".OrderlyReceiver3">
<intent-filter android:priority="998">
<action android:name="com.yuzhentao.orderlyaction" />
</intent-filter>
</receiver>

最后是点击Button发送广播

case R.id.button_orderly_activity_main:
Intent intentOrderly = new Intent(Constants.ACTION_ORDERLY);
intentOrderly.putExtra(Constants.MSG_ORDERLY, "收到有序广播");
sendOrderedBroadcast(intentOrderly, "yuzhentao.permission.MY_BROADCAST_PERMISSION");
//                sendOrderedBroadcast(intentOrderly, null);
break;

运行,打印Log

02-28 11:19:19.373 25914-25914/com.yuzhentao.broadcastreceiverdemo E/OrderlyReceiver1: 收到有序广播

02-28 11:19:19.375 25914-25914/com.yuzhentao.broadcastreceiverdemo E/OrderlyReceiver2: 收到有序广播@OrderlyReceiver1

02-28 11:19:19.377 25914-25914/com.yuzhentao.broadcastreceiverdemo E/OrderlyReceiver3: 收到有序广播@OrderlyReceiver1@OrderlyReceiver2

如果OrderlyReceiver1使用了

abortBroadcast();

效果如下

02-28 11:19:19.373 25914-25914/com.yuzhentao.broadcastreceiverdemo E/OrderlyReceiver1: 收到有序广播

好了,到此结束。希望对大家有所帮助。

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: