android AsyncTask封装

AsyncTask介绍:
1. 意义,不多说了,网上太多。
2. 建议,看看AsyncTask的源代码,其实简单
3. 代码:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* async task manager factory
*
* @author wei.zhou
*
*/
public class AsyncTaskFactory {

private final static String TAG = "AsyncTaskFactory # init";

/**
* Asynchronous task to return the results after treatment
*
* @author wei.zhou
*
*/
public static class AsyncResult {
// url
public String _url;
// byte[]
public byte[] _byteBuffer;
}

/**
* Callback processing is complete
* @author wei.zhou
*
*/
public static interface IProgressCallback{

/**
* Time-consuming operation
*/
public void progressCallback(AsyncResult result) ;
}

/**
* Asynchronous callback interface after the task
*
* @author wei.zhou
*
*/
public static interface IResultCallback {

/**
* Synchronous Callback
*
* @param result
*/
public void resultCallback(AsyncResult result);

}

// //
// ***************
// //
/**
* Asynchronous downloads package data downloaded from the network and returns the result as a byte [] array
*
* @author wei.zhou
*
*/
private class AsyncDownLoaderTask extends android.os.AsyncTask<String, Integer, AsyncResult> {

/** synchronous Callback */
private IResultCallback _resultCallback;

/** ProgressCallback */
private IProgressCallback _progressCallback ;

/**
* The parameters of the implementation of,url...., the method running in the asynctask thread
*/
@Override
protected AsyncResult doInBackground(String... params) {
final AsyncResult result = new AsyncResult();
result._url = params[0];
result._byteBuffer = download(params[0]);
// callback processing
if(_progressCallback != null)
_progressCallback.progressCallback(result) ;
//
return result;
}

/**
* call by main thread
* Do not do anything in the time-consuming method of operation within
* The results
*/
@Override
protected void onPostExecute(AsyncResult result) {
// After the implementation of the callback
if (_resultCallback != null)
_resultCallback.resultCallback(result);
}

/**
*
*/
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}

/**
* The implementation of the callback interface to receive
*
* @param callback
* @param params
*/
public final void execute(IProgressCallback proCallback,IResultCallback callback, String... params) {
//
_progressCallback = proCallback ;
//
_resultCallback = callback;
//
execute(params);
}

/**
* Download Data
*
* @param _url
* @return
*/
public final byte[] download(String _url) {
URL url = null;
HttpURLConnection conn = null;
InputStream input = null;
ByteArrayOutputStream out = null;
byte[] r_buffer = null;
try {
url = new URL(_url);
conn = (HttpURLConnection) url.openConnection();
input = conn.getInputStream();
out = new ByteArrayOutputStream();
//
byte buf[] = new byte[1024 * 256];
int len;
//
while ((len = input.read(buf)) > 0) {
out.write(buf, 0, len);
}
r_buffer = out.toByteArray();
//
out.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
url = null;
conn = null;
input = null;
out = null;
}
//
return r_buffer;
}
}

/** */
private static AsyncTaskFactory _instance;

/**
* Private constructor
*/
private AsyncTaskFactory() {
System.out.println(TAG);
}

/**
* Get instance object
* @return
*/
public static final AsyncTaskFactory getInstance() {
if (_instance == null)
_instance = new AsyncTaskFactory();
//
return _instance;
}

/**
* Add asynchronous task
*
* @param proCallback : Time-consuming operation, read, write IO......,call by async task thread
* @param resCallback : Not time-consuming operation, call by main UI thread
* @param params:url1
*/
public final void addSyncTask(IProgressCallback proCallback,IResultCallback resCallback, String... params) {
new AsyncDownLoaderTask().execute(proCallback,resCallback, params);
}
}

4. 使用方式:
asyncFactory.addSyncTask(new AsyncTaskFactory.IProgressCallback() {
// 耗时操作,该方法是在syncTask的线程中调用。
@Override
public void progressCallback(AsyncResult result) {
// 下载完数据后的操作,如,写到文件中,设置对象的变量.....
}

},new AsyncTaskFactory.IResultCallback() {
// 该方法是被UI线程调用
@Override
public void resultCallback(AsyncResult result) {
// 把该数据设置到对应的UI上,如,通过byte[] 创建 bitmap
}
}, url[i]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值