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

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

			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这样的组件的创建销毁方法进行关联,以达到我们更简便的目的。

你可能感兴趣的:((Android开发辅助工具)动态广播注册解注工具)