当前位置: 首页 > 图灵资讯 > 技术篇> LocalBroadcastManager的归总类

LocalBroadcastManager的归总类

来源:图灵教育
时间:2023-05-30 09:40:31

package com.istarshine.broadcast;import android.content.BroadcastReceiver;import android.content.Intent;import android.content.IntentFilter;import android.support.v4.content.LocalBroadcastManager;import com.istarshine.base.BaseApplication;public class LocalBroadcastUtils {    public static void register(BroadcastReceiver mBroadcastReceiver, IntentFilter intentFilter) {        LocalBroadcastManager.getInstance(BaseApplication.getAppContextBase()).registerReceiver(mBroadcastReceiver, intentFilter);    }    public static void register(BroadcastReceiver mBroadcastReceiver, String... action) {        IntentFilter intentFilter = new IntentFilter();        for (String a : action) {            intentFilter.addAction(a);        }        register(mBroadcastReceiver, intentFilter);    }    public static void unRegister(BroadcastReceiver mBroadcastReceiver) {        try {            LocalBroadcastManager.getInstance(BaseApplication.getAppContextBase()).unregisterReceiver(mBroadcastReceiver);        } catch (Exception e) {            e.printStackTrace();        }    }    public static void sendBroadcast(String action) {        sendBroadcast(new Intent(action));    }    public static void sendBroadcast(String... actions) {        for (String action : actions) {            sendBroadcast(new Intent(action));        }    }    public static void sendBroadcast(Intent intent) {        LocalBroadcastManager.getInstance(BaseApplication.getAppContextBase()).sendBroadcast(intent);    }}