package com.archer.rainbow.AsyncTaskDemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class AsyncTaskDemoActivity extends Activity {
private Button writeLog;
private TextView log;
// 滚动条
private ProgressBar dlProgress;
private Button positiveBtn;
private Button negativeBtn;
private MyAsyncTask mat;
private boolean taskOver = false;
private OnClickListener btnListener = new OnClickListener() {
@Override
public void onClick(View v) {
mat = new MyAsyncTask();
// 从download.xml加载画面
LayoutInflater factory = LayoutInflater
.from(AsyncTaskDemoActivity.this);
final View downloadView = factory.inflate(R.layout.download, null);
log = (TextView) downloadView.findViewById(R.id.log);
dlProgress = (ProgressBar) downloadView
.findViewById(R.id.dlProgress);
AlertDialog dialog = new AlertDialog.Builder(
AsyncTaskDemoActivity.this)
.setTitle("DownLoad Info")
.setView(downloadView)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
if (!taskOver) {
mat.cancel(true);
}
((AlertDialog)dialog).show();
}
}).create();
dialog.show();
// 此处获取 Button 的代码必须写在 show() 方法之后,否则会报 空指针 异常,
// 这可能是系统Bug,请参见
// http://code.google.com/p/android/issues/detail?id=6360
positiveBtn = dialog.getButton(AlertDialog.BUTTON1);
positiveBtn.setEnabled(false);
negativeBtn = dialog.getButton(AlertDialog.BUTTON2);
mat.execute(10);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
writeLog = (Button) findViewById(R.id.writeLog);
writeLog.setOnClickListener(btnListener);
}
private class MyAsyncTask extends AsyncTask<Integer, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
updateLog("AsyncTask started");
}
@Override
protected Boolean doInBackground(Integer... params) {
int max = params[0];
for (int i = 0; i < max; i++) {
try {
publishProgress((int) (((i + 1.0) / max) * 100));
// 每隔一秒钟执行一次,模拟下载文件
Thread.sleep(1000);
} catch (InterruptedException e) {
return false;
}
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dlProgress.setProgress(values[0]);
updateLog(log.getText() + ".");
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
taskOver = true;
positiveBtn.setEnabled(true);
negativeBtn.setEnabled(false);
if (result) {
updateLog("AsyncTask ended. Download successfully");
} else {
updateLog("AsyncTask ended. Download failed");
}
}
@Override
protected void onCancelled() {
super.onCancelled();
taskOver = true;
positiveBtn.setEnabled(true);
negativeBtn.setEnabled(false);
updateLog("AsyncTask cancelled");
}
}
private void updateLog(String text) {
log.setText(text);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
AsyncTask的简单例子
共27个文件
class:11个
xml:4个
png:3个
5星 · 超过95%的资源 需积分: 38 86 下载量 22 浏览量
2012-03-23
14:56:13
上传
评论 1
收藏 57KB ZIP 举报
温馨提示
AsyncTask的一个小例子,详情请见: http://blog.csdn.net/woaieillen/article/details/7372293
资源推荐
资源详情
资源评论
收起资源包目录
AsyncTaskDemo.zip (27个子文件)
AsyncTaskDemo
AndroidManifest.xml 707B
res
layout
download.xml 939B
main.xml 379B
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 3KB
drawable-hdpi
icon.png 4KB
values
strings.xml 115B
proguard.cfg 1KB
src
com
archer
rainbow
AsyncTaskDemo
AsyncTaskDemoActivity.java 4KB
bin
com
archer
rainbow
AsyncTaskDemo
R.class 629B
AsyncTaskDemoActivity$1$2.class 2KB
R$layout.class 468B
AsyncTaskDemoActivity$MyAsyncTask.class 4KB
R$attr.class 382B
R$id.class 517B
R$string.class 440B
AsyncTaskDemoActivity.class 4KB
AsyncTaskDemoActivity$1.class 4KB
AsyncTaskDemoActivity$1$1.class 984B
R$drawable.class 442B
classes.dex 8KB
AsyncTaskDemo.apk 17KB
resources.ap_ 11KB
.classpath 280B
assets
default.properties 362B
.project 849B
gen
com
archer
rainbow
AsyncTaskDemo
R.java 909B
共 27 条
- 1
资源评论
- zy1826382015-04-30好用,额可以借鉴代码
- qq_168008372015-09-29简单易懂,好例子!
- qq8520140982015-11-27不错啊! 这个非常好啊!推荐。
- LindaHXX2013-09-23例子很好,很有帮助,学习了谢谢
woaieillen
- 粉丝: 64
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功