Android AsyncTask 异步运用
2012-09-05 13:58:34 来源:WEB开发网package com.example.hand;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends
Activity {
ProgressDialog progressDialog;
TextView mTextView;
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
findViewById(R.id.button1)
.setOnClickListener(
new OnClickListener() {
public void onClick(
View v) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
LoadTask loadtask = new LoadTask();
loadtask.execute();
}
});
}
public class LoadTask
extends
AsyncTask<Void, Integer, String> {
private ProgressDialog progressDialog;
@Override
protected String doInBackground(
Void... params) {
// TODO Auto-generated method stub
int sum = 1;
while (sum <= 10) {
try {
Thread.sleep(500);
publishProgress(sum);
sum++;
}
catch (Exception e) {
// TODO: handle exception
}
}
return "Loading Finished";
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog = ProgressDialog
.show(MainActivity.this,
"demo",
"loading......",
true);
}
@Override
protected void onPostExecute(
String result) {
// TODO Auto-generated method stub
progressDialog.dismiss();
mTextView.setText(result);
}
@Override
protected void onProgressUpdate(
Integer... values) {
// TODO Auto-generated method stub
mTextView.setText(values[0]
* 10 + "%");
}
}
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接