1.顯示進度框和移除進度框
/** 加載框的文字說明. */
private String mProgressMessage = "請稍候...";
/** 全局的加載框對象,已經完成初始化. */
public ProgressDialog mProgressDialog;
/**
* 描述:顯示進度框.
*/
public void showProgressDialog() {
showProgressDialog(null);
}
/**
* 描述:顯示進度框.
* @param message the message
*/
public void showProgressDialog(String message) {
// 創建一個顯示進度的Dialog
if(!AbStrUtil.isEmpty(message)){
mProgressMessage = message;
}
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
// 設置點擊屏幕Dialog不消失
mProgressDialog.setCanceledOnTouchOutside(false);
}
mProgressDialog.setMessage(mProgressMessage);
showDialog(AbConstant.DIALOGPROGRESS);
}
/**
* 描述:移除進度框.
*/
public void removeProgressDialog() {
removeDialog(AbConstant.DIALOGPROGRESS);
}
2.Toast提示文本
/**
* 描述:Toast提示文本.
* @param text 文本
*/
public void showToast(String text) {
Toast.makeText(this,""+text, Toast.LENGTH_SHORT).show();
}
/**
* 描述:Toast提示文本.
* @param resId 文本的資源ID
*/
public void showToast(int resId) {
Toast.makeText(this,""+this.getResources().getText(resId), Toast.LENGTH_SHORT).show();
}
3.在線程中提示文本信息
/**
* 描述:在線程中提示文本信息.
* @param resId 要提示的字符串資源ID,消息what值爲0,
*/
public void showToastInThread(int resId) {
Message msg = baseHandler.obtainMessage(0);
Bundle bundle = new Bundle();
bundle.putString("Msg", this.getResources().getString(resId));
msg.setData(bundle);
baseHandler.sendMessage(msg);
}
/**
* 描述:在線程中提示文本信息.
* @param toast 消息what值爲0
*/
public void showToastInThread(String toast) {
Message msg = baseHandler.obtainMessage(0);
Bundle bundle = new Bundle();
bundle.putString("Msg", toast);
msg.setData(bundle);
baseHandler.sendMessage(msg);
}
4.線程池
/**
* The Class AbAppData.
*/
public class AbAppData {
/** 日誌開關. */
public static boolean DEBUG = false;
/** 性能測試. */
public static boolean mMonitorOpened = false;
/** 起始執行時間. */
public static long startLogTimeInMillis = 0;
}
/**
*
* Copyright (c) 2012 All rights reserved
* 名稱:AbTaskPool.java
* 描述:線程池,程序中只有1個
* @author zhaoqp
* @date:2013-5-23 上午10:10:53
* @version v1.0
*/
public class AbTaskPool{
/** The tag. */
private static String TAG = "AbTaskPool";
/** The Constant D. */
private static final boolean D = AbAppData.DEBUG;
/** 單例對象 The http pool. */
private static AbTaskPool mAbTaskPool = null;
/** 固定5個線程來執行任務. */
private static int nThreads = 5;
/** 線程執行器. */
private static ExecutorService executorService = null;
/** 下載完成後的消息句柄. */
private static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
AbTaskItem item = (AbTaskItem)msg.obj;
if(item.getListener() instanceof AbTaskListListener){
((AbTaskListListener)item.listener).update((List<?>)item.getResult());
}else if(item.getListener() instanceof AbTaskObjectListener){
((AbTaskObjectListener)item.listener).update(item.getResult());
}else{
item.listener.update();
}
}
};
/**
* 初始化線程池
*/
static{
nThreads = AbAppUtil.getNumCores();
mAbTaskPool = new AbTaskPool(nThreads*5);
}
/**
* 構造線程池.
*
* @param nThreads 初始的線程數
*/
protected AbTaskPool(int nThreads) {
executorService = Executors.newFixedThreadPool(nThreads);
}
/**
* 單例構造圖片下載器.
*
* @return single instance of AbHttpPool
*/
public static AbTaskPool getInstance() {
return mAbTaskPool;
}
/**
* 執行任務.
* @param item the item
*/
public void execute(final AbTaskItem item) {
executorService.submit(new Runnable() {
public void run() {
try {
//定義了回調
if (item.listener != null) {
item.listener.get();
//交由UI線程處理
Message msg = handler.obtainMessage();
msg.obj = item;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
*
* 描述:獲取線程池的執行器
* @return executorService
* @throws
*/
public static ExecutorService getExecutorService() {
return executorService;
}
/**
* 描述:立即關閉.
*/
public void shutdownNow(){
if(!executorService.isTerminated()){
executorService.shutdownNow();
listenShutdown();
}
}
/**
* 描述:平滑關閉.
*/
public void shutdown(){
if(!executorService.isTerminated()){
executorService.shutdown();
listenShutdown();
}
}
/**
* 描述:關閉監聽.
*/
public void listenShutdown(){
try {
while(!executorService.awaitTermination(1, TimeUnit.MILLISECONDS)) {
if(D) Log.d(TAG, "線程池未關閉");
}
if(D) Log.d(TAG, "線程池已關閉");
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.數據執行單位
public class AbTaskItem {
/** 記錄的當前索引. */
public int position;
/** 執行完成的回調接口. */
public AbTaskListener listener;
/** 執行完成的結果. */
private Object result;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public AbTaskListener getListener() {
return listener;
}
public void setListener(AbTaskListener listener) {
this.listener = listener;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
6.任務執行的控制父類
public class AbTaskListener {
/**
* 描述:執行開始後調用.
*/
public void get(){};
/**
* 描述:執行開始後調用.
*/
public void update(){};
}
7.具體實現
//線程池
poolBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//顯示進度框
showProgressDialog();
AbTaskPool mAbTaskPool = AbTaskPool.getInstance();
//定義異步執行的對象
final AbTaskItem item = new AbTaskItem();
item.listener = new AbTaskListener() {
@Override
public void update() {
removeProgressDialog();
showToast("執行完成");
}
@Override
public void get() {
try {
showToastInThread("開始執行");
Thread.sleep(1000);
//下面寫要執行的代碼,如下載數據
} catch (Exception e) {
}
};
};
//開始執行
mAbTaskPool.execute(item);
}
});