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