- 授权协议: Apache
- 开发语言: Java
- 操作系统: Android
- 软件首页: http://snowdream.github.io/android-multithread/
- 软件文档: http://snowdream.github.io/android-multithread/
- 官方下载: https://github.com/snowdream/android-multithread
软件介绍
一个android平台上的 扩展任务库,在AsyncTask基础上进行扩展。
用法
1.继承 com.github.snowdream.android.util.concurrent.AsyncTask
//inherit a class from com.github.snowdream.android.util.concurrent.AsyncTask
public class DownloadFilesTask extends AsyncTask {
public DownloadFilesTask(TaskListener listener) {
//explicit inherit the construction from the super class.
super(listener);
}
/**
* TODO
* if error occurs,carry it out.
*
* if (listener != null) {
* listener.onError(new Throwable());
* }
*/
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += 10;
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return totalSize;
}
}
2.定义一个 TaskListener.其中的第一个泛型参数是返回结果类型,第二个泛型参数是任务进度的类型。
private TaskListener listener = new TaskListener(){
@Override
public void onStart() {
super.onStart();
Log.i("onStart()");
}
@Override
public void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("onProgressUpdate(values)" + values[0] );
}
@Override
public void onSuccess(Long result) {
super.onSuccess(result);
Log.i("onSuccess(result)" + result);
}
@Override
public void onCancelled() {
super.onCancelled();
Log.i("onCancelled()");
}
@Override
public void onError(Throwable thr) {
super.onError(thr);
Log.i("onError()");
}
@Override
public void onFinish() {
super.onFinish();
Log.i("onFinish()");
}
};
3.创建一个AsyncTask任务,并且执行。
URL url = null;
try {
url = new URL("http://www.baidu.com/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new DownloadFilesTask(listener).execute(url,url,url);
Effective Java 中文版
(美)Joshua Bloch / 潘爱民 / 机械工业出版社 / 2003-1 / 39.00元
本书介绍了在Java编程中57条极具实用价值的经验规则,这些经验规则涵盖了大多数开发人员每天所面临的问题的解决方案。通过对Java平台设计专家所使用的技术的全面描述,揭示了应该做什么,不应该做什么才能产生清晰、健壮的高效的代码。 本书中的每条规则都以简短、独立的小文章形式出现,这些小文章包含了详细而精确的建议,以及对语言中许多细微之处的深入分析,并通过例子代码加以进一步说明。贯穿全书的是通用......一起来看看 《Effective Java 中文版》 这本书的介绍吧!
