android线程通信之Asynctask

这篇文章我们来谈谈android线程通信中的Asynctask,首先这是一个抽象的类,如果我们要使用它,我们必须有一个它的实现子类,并重写它的四个方法,在我的代码之中,我对每个方法都有详细的介绍,因为一个Asynctask只能execute一次,所以我的这个计数程序有点小问题,你们可以通过暂停等按钮的不可点击,然后再启动按钮以后再恢复按钮的点击,当然启动按钮会重新给一个新的对象,我把代码贴出来大家参考。当然,和以往一样,布局代码就不贴了,只贴java代码

package com.jk.asynctaskdemo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	//declare variable object
	TextView tv;
	MyTask myTask;
	Button btn_start, btn_begin, btn_pause, btn_stop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	public void init() {
		//bundle the widget with the id
		tv = (TextView) findViewById(R.id.tv_count);
		btn_start = (Button) findViewById(R.id.btn_start);
		btn_begin = (Button) findViewById(R.id.btn_begin);
		btn_pause = (Button) findViewById(R.id.btn_pause);
		btn_stop = (Button) findViewById(R.id.btn_stop);
		//set the onclick event
		btn_start.setOnClickListener(this);
		btn_begin.setOnClickListener(this);
		btn_pause.setOnClickListener(this);
		btn_stop.setOnClickListener(this);
		//instantiation myTask
		myTask = new MyTask();
		

	}

	@Override
	public void onClick(View v) {
		//judge different click event
		switch (v.getId()) {
		case R.id.btn_start:
			
			myTask.isrun = true;
			myTask.execute(0);
			break;
		case R.id.btn_begin:
			myTask.ispause = false;
			break;
		case R.id.btn_pause:
			myTask.ispause = true;
			break;
		case R.id.btn_stop:
			myTask.isrun = false;
		}

	}
//first params,the type of the parameter that you need to send when you launch the task
//second progress,the type of the parameter that be send when the onProgressUpdata method is running
//third result,the type of doInBackground method return,and the onPostExecute receive
	class MyTask extends AsyncTask<Integer, Integer, Integer> {
		int i = 0;
		boolean isrun;
		boolean ispause;
        //the method is running in the subthread,always run the longtime task
		@Override
		protected Integer doInBackground(Integer... params) {
			while (isrun) {
				while(ispause){
					
				}
				//call the onProgressUpdata method
				publishProgress(++i);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return null;
		}
         //running in main thread ,and running after doInBackground
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
        //running in main thread,and running before doInBackground
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}
         //running in main thread,and called by publishProgress
		@Override
		protected void onProgressUpdate(Integer... values) {
			tv.setText(""+values[0]);
			super.onProgressUpdate(values);
		}

	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值