(Android开发辅助工具)动态广播注册解注工具-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

(Android开发辅助工具)动态广播注册解注工具

简介: 平常我们开发的时候需要使用到动态注册广播,如果在一个类内注册很多的广播代码就会既冗余又乱糟糟的,就像这样: msgReceiver = new NewMessageBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(EMChatManager.

平常我们开发的时候需要使用到动态注册广播,如果在一个类内注册很多的广播代码就会既冗余又乱糟糟的,就像这样:

			msgReceiver = new NewMessageBroadcastReceiver();
			IntentFilter intentFilter = new IntentFilter(EMChatManager.getInstance().getNewMessageBroadcastAction());
			registerReceiver(msgReceiver, intentFilter);

			IntentFilter ackMessageIntentFilter = new IntentFilter(EMChatManager.getInstance()
					.getAckMessageBroadcastAction());
			registerReceiver(ackMessageReceiver, ackMessageIntentFilter);

			IntentFilter cmdMessageIntentFilter = new IntentFilter(EMChatManager.getInstance()
					.getCmdMessageBroadcastAction());
			registerReceiver(cmdMessageReceiver, cmdMessageIntentFilter);
			newMessageBroadcastReceiver = new NewMessageTipBroadcastReceiver();
			IntentFilter filter = new IntentFilter(StringConstants.NewMessageBroadcastReceiver);
			filter.addCategory(Intent.CATEGORY_DEFAULT);
			registerReceiver(newMessageBroadcastReceiver, filter);

			disTipBroadcastReceiver = new DisTipBroadcastReceiver();
			filter = new IntentFilter(StringConstants.DisTipBroadcastReceiver);
			filter.addCategory(Intent.CATEGORY_DEFAULT);
			registerReceiver(disTipBroadcastReceiver, filter);

上面是我们的注册部分,除了我们需要将广播的引用升级为类的成员之外,我们还需要添加解注代码,就像下面这样:

			if (newMessageBroadcastReceiver != null) {
				unregisterReceiver(newMessageBroadcastReceiver);
			}
			if (disTipBroadcastReceiver != null) {
				unregisterReceiver(disTipBroadcastReceiver);
			}
			if (mJPushReceiver != null) {
				unregisterReceiver(mJPushReceiver);
			}

			if (newMessageBroadcastReciver != null) {
				unregisterReceiver(newMessageBroadcastReciver);
			}
			if (newTeacherMessageBroadcastReciver != null) {
				unregisterReceiver(newTeacherMessageBroadcastReciver);
			}
			if (newMessageUserCenterBroadcastReciver != null) {
				unregisterReceiver(newMessageUserCenterBroadcastReciver);
			}
			if (mUIRefreshReceiver != null) {
				unregisterReceiver(mUIRefreshReceiver);
			}

所以,我们需要一种极为简便的工具来完成这些工作,让我们开发的时候不用去关心那么多不重要的东西:

package com.sahadev.commontools;

import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.util.ArrayMap;

/**
 * 广播注册解注工具
 * 
 * @author Sahadev
 *
 */
public class RegisterReceiverUtils {
	private static Map<Class<?>, BroadcastReceiver> MAPS = new ArrayMap<Class<?>, BroadcastReceiver>();

	/**
	 * @param context
	 * @param broadcastReceiverClass
	 * @param action
	 */
	public static void registerBroadcastReceiver(Context context, Class<?> broadcastReceiverClass, String action) {
		IntentFilter filter = new IntentFilter(action);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		try {

			BroadcastReceiver broadcastReceiver = (BroadcastReceiver) broadcastReceiverClass.newInstance();
			if (broadcastReceiver != null) {
				MAPS.put(broadcastReceiverClass, broadcastReceiver);
				context.registerReceiver(broadcastReceiver, filter);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public static void unregisterBroadcastReceiver(Context context, Class<?> broadcastReceiverClass) {
		BroadcastReceiver broadcastReceiver = MAPS.get(broadcastReceiverClass);
		context.unregisterReceiver(broadcastReceiver);
		MAPS.remove(broadcastReceiverClass);

	}

}

上面这个工具只是简单的对注册解注做了简单的封装,如果可以,我们可以开放开启与关闭方法,与Activity这样的组件的创建销毁方法进行关联,以达到我们更简便的目的。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章