AndroidBroadcast做的簡單封裝:

請叫我東子發表於2016-02-19

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u010046908/article/details/50697441

  

在開發過程中,突然發現廣播的使用非常的多,因此作出如下三步使用流程,並封裝一個BoradcastManager類:

 1 、在任何地方傳送廣播

BroadcastManager.getInstance(mContext).sendBroadcast(FindOrderActivity.ACTION_RECEIVE_MESSAGE);

2、頁面在onCreate()中初始化廣播

BroadcastManager.getInstance(mContext).addAction(ACTION_RECEIVE_MESSAGE, new BroadcastReceiver(){
 @Override
 public void onReceive(Context arg0, Intent intent) {
  String command = intent.getAction();
 if(!TextUtils.isEmpty(command)){
  if((ACTION_RECEIVE_MESSAGE).equals(command)){
  //獲取json結果
  String json = intent.getStringExtra("result");
  //做你該做的事情
  }
  }
 }
 });

3、頁面在ondestory銷燬廣播

BroadcastManager.getInstance(mContext).destroy(ACTION_RECEIVE_MESSAGE);

4、BroadcastManager的實現;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


import com.chni.hms_library.util.Log;


import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName:BroadcastManager
 * @描述:廣播管理者
 * @author lidong on 2016/2/18.
 * @使用方法:
 * (1)在任何地方傳送廣播
 * BroadcastManager.getInstance(mContext).sendBroadcast(FindOrderActivity.ACTION_RECEIVE_MESSAGE);
 * (2)頁面在onCreate()中初始化廣播
 * BroadcastManager.getInstance(mContext).addAction(ACTION_RECEIVE_MESSAGE, new BroadcastReceiver(){
 * @Override
 * public void onReceive(Context arg0, Intent intent) {
 * String command = intent.getAction();
 * if(!TextUtils.isEmpty(command)){
 * if((ACTION_RECEIVE_MESSAGE).equals(command)){
 * //獲取json結果
 * String json = intent.getStringExtra("result");
 * //做你該做的事情
 * }
 * }
 * }
 * });
 * (3)頁面在ondestory銷燬廣播
 * BroadcastManager.getInstance(mContext).destroy(ACTION_RECEIVE_MESSAGE);
 */
public class BroadcastManager {


    private Context mContext;
    private static BroadcastManager instance;
    private Map<String, BroadcastReceiver> receiverMap;


    /**
     * 構造方法
     *
     * @param context
     */
    private BroadcastManager(Context context) {
        this.mContext = context;
        receiverMap = new HashMap<String, BroadcastReceiver>();
    }


    /**
     * [獲取BroadcastManager例項,單例模式實現]
     *
     * @param context
     * @return
     */
    public static BroadcastManager getInstance(Context context) {
        if (instance == null) {
            synchronized (BroadcastManager.class) {
                if (instance == null) {
                    instance = new BroadcastManager(context);
                }
            }
        }
        return instance;
    }


    /**
     * 新增Action,做廣播的初始化
     *
     * @param
     */
    public void addAction(String action, BroadcastReceiver receiver) {
        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction(action);
            mContext.registerReceiver(receiver, filter);
            receiverMap.put(action, receiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 新增Action,做廣播的初始化
     *
     * @param
     */
    public void addAction(String[] actions, BroadcastReceiver receiver) {
        try {
            IntentFilter filter = new IntentFilter();
            for(String action:actions){
                filter.addAction(action);
            }
            mContext.registerReceiver(receiver, filter);
            for(String action:actions) {
                receiverMap.put(action, receiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 傳送廣播
     *
     * @param action 唯一碼
     */
    public void sendBroadcast(String action) {
        sendBroadcast(action, "");
    }


    /**
     * 傳送廣播
     *
     * @param action 唯一碼
     * @param obj    引數
     */
    public void sendBroadcast(String action, Object obj) {
        try {
            Intent intent = new Intent();
            intent.setAction(action);
            intent.putExtra("result", obj.toString());
            mContext.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 銷燬廣播
     * @param actions
     */
    public void destroy(String... actions) {
        try {
            if (receiverMap != null) {
                for(String action:actions){
                    BroadcastReceiver receiver = receiverMap.get(action);
                    if (receiver != null) {
                        mContext.unregisterReceiver(receiver);
                    }
                }
            }
        } catch (IllegalArgumentException e){
            Log.d("Broadcastmanager",e.toString());
        }
    }
}

基本上就這些了吧,歡迎大家提建議


相關文章