您的位置:首页 > 移动开发 > Android开发

android线程通信之Asynctask

2016-04-22 09:40 465 查看
这篇文章我们来谈谈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);
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: