AndroidBroadcast做的簡單封裝:
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 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());
}
}
}
基本上就這些了吧,歡迎大家提建議
相關文章
- Retrofit的簡單封裝封裝
- IQueryable的簡單封裝封裝
- ProgressFragment的簡單封裝Fragment封裝
- BaseActivity與BaseFragment的簡單封裝Fragment封裝
- 一個簡單的 Amqp 封裝MQ封裝
- Fetch API 簡單封裝API封裝
- 二維碼簡單封裝封裝
- Golang 對MongoDB的操作簡單封裝GolangMongoDB封裝
- JS棧結構的簡單封裝JS封裝
- 陣列常用方法的簡單封裝陣列封裝
- 一個封裝簡單使用簡單的雷達檢視封裝
- 5-AVI–Fragment簡單封裝Fragment封裝
- iOS 基於FMDB簡單封裝iOS封裝
- 對getElementsByTagName()方法簡單封裝封裝
- 簡單問題,封裝和框架!封裝框架
- 簡單封裝get和jsonp封裝JSON
- 封裝一個簡單的日曆元件封裝元件
- 封裝一個簡單的動畫函式封裝動畫函式
- c# Quartz.net的簡單封裝C#quartz封裝
- 一個最簡單的類JQuery封裝jQuery封裝
- 微信小程式wx.request的簡單封裝微信小程式封裝
- javascript物件導向的封裝簡單介紹JavaScript物件封裝
- Kotlin中Retrofit與RxJava的簡單封裝KotlinRxJava封裝
- SqlSugar 多資料來源的簡單封裝SqlSugar封裝
- Flutter Dio http簡單封裝與使用FlutterHTTP封裝
- 基於Promise實現對Ajax的簡單封裝Promise封裝
- 我自己封裝的 Laravel 融雲 sdk,簡單好用封裝Laravel
- python-對requests請求簡單的封裝Python封裝
- web自動化框架—BasePage 類的簡單封裝Web框架封裝
- MongoDB Python官方驅動 PyMongo 的簡單封裝MongoDBPython封裝
- 把redux封裝起來,更加簡單方便的使用Redux封裝
- 小程式wx.request()方法簡單封裝封裝
- 簡單封裝分頁功能pageView.js封裝ViewJS
- javascript物件導向封裝簡單介紹JavaScript物件封裝
- Flutter 封裝:富文字 RichText 極簡封裝Flutter封裝
- 簡訊介面封裝封裝
- 商城購物車加減控制元件的簡單封裝控制元件封裝
- 在 vue-cil 裡封裝一個簡單的 axiosVue封裝iOS