AsyncTask类的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在修改UI组件时,之前我一般用开启匿名子线程+Handler来实现的,这种方式效率低,代码看上去也非常臃肿,在这里引入AsyncTask,其特点是任务在主线程之外运行,而回调方法是在主线程中执行, 这就有效地避免了使用Handler带来的麻烦,AsyncTask是使用java.util.concurrent 框架来管理线程以及任务的执行的,concurrent框架是一个非常成熟,高效的框架,经过了严格的测试。这说明AsyncTask的设计很好的解决了匿名线程存在的问题。下面我们用一个简单例子来演示AsyncTask的使用,实现不断更新进度条的进度,代码如下(应朋友们的要求,以后会尽量附上图片,嘻嘻~):

Activity:

package com.home.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.home.asynctask.R;

public class AsyncTaskTestActivity extends Activity implements OnClickListener {
	private ProgressDialog dialog;
	private Button progressBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		progressBtn = (Button) findViewById(R.id.main_btn_open_dialog);
		progressBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 显示对话框,调用此方法后,会回调下面的onCreateDialog方法
		showDialog(0);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == 0) {
			// 创建进度对话框
			dialog = new ProgressDialog(this);
			// 设置最大进度数
			dialog.setMax(100);
			// 设置标题
			dialog.setTitle("进度对话框");
			// 设置进度对话框显示的内容
			dialog.setMessage("正在加载进度");
			// 设置对话框风格为水平显示进度条
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			// 显示进度对话框
			dialog.show();
			// 启动任务
			MyTask task = new MyTask();
			// 执行任务,将会调用MyTask的doInBackground方法
			task.execute(null);
		}
		return super.onCreateDialog(id);
	}

	/**
	 * AsyncTask 任务类
	 * 
	 * @author Administrator
	 * 
	 */
	public class MyTask extends AsyncTask<String, Integer, Boolean> {
		// 子线程运行的方法
		@Override
		protected Boolean doInBackground(String... params) {

			for (int i = 0; i <= 100; i++) {
				// 更新进度条,会回调下面onProgressUpdate方法进行更新
				publishProgress(i);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

		// 每次调用publishProgress时调用该方法
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			// 设置进度对话框的进度
			dialog.setProgress(values[0]);
		}

		// 当doInBackground方法结束后调用该方法
		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			dialog.setMessage("进度完成");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// 把进度条清零
			dialog.setProgress(0);
			dialog.cancel();
		}
	}

}

布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/main_btn_open_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开对话框" />

</LinearLayout>

附上图片:


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值