AsyncTask使UI线程的使用变得恰当和简单。这个类允许在后台执行操作并且在UI线程呈现处理的结果,无需操作线程。
一个AsyncTask必须要被继承才能使用。这个子类必须重写doInBackground(Params...)方法,通常还要重写onPostExecute(Result)方法。
[mw_shl_code=java,true]public class AsyncTaskTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set the six buttons listener
Button startButton = (Button) this.findViewById(R.id.StartTask);
final TestAsyncTask task = new TestAsyncTask(0);
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
task.execute("str1", "str2");
}
});
Button endButton = (Button) this.findViewById(R.id.StopTask);
endButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
task.cancel(false);
}
});
Button startSleepButton = (Button) this
.findViewById(R.id.StartThread_sleep);
final ThreadForTestSleep threadForTestSleep = new ThreadForTestSleep();
startSleepButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
threadForTestSleep.start();
}
});
Button endSleepButton = (Button) this
.findViewById(R.id.StopThread_sleep);
endSleepButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
threadForTestSleep.interrupt();
}
});
Button startWaitButton = (Button) this
.findViewById(R.id.StartThread_wait);
final ThreadForTestWait threadForTestWait = new ThreadForTestWait();
startWaitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
threadForTestWait.start();
}
});
Button endWaitButton = (Button) this.findViewById(R.id.StopThread_wait);
endWaitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
threadForTestWait.interrupt();
}
});
}
/**
* AsyncTask
*
* @author alex
*
*/
private class TestAsyncTask extends AsyncTask<String, Integer, Double> {
double a;
public TestAsyncTask(double a) {
this.a = a;
}
@Override
protected Double doInBackground(String... params) {
for (String param : params) {
Log.i("TestAsyncTask", "param:" + param);
}
Log.i("TestAsyncTask", "doInBackground is start");
for (int i = 0; i < 10000000; i++) {
a = i * i + i;
Log.d("-----", "a:" + a);
}
Log.i("TestAsyncTask", "sleep 1 is end");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("TestAsyncTask", "sleep 2 is end and continue execute");
return a;
}
protected void onPostExecute(Double result) {
Log.i("last a value is", "" + result);
}
}
/**
* test sleep
*
* @author Administrator
*
*/
private class ThreadForTestSleep extends Thread {
public void run() {
Log.i("ThreadForTestWait", "sleep start");
try {
sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double a;
for (int i = 0; i < 10000000; i++) {
a = i * i + i;
Log.d("-----", "a:" + a);
}
Log.i("ThreadForTestWait", "sleep end");
}
}
/**
* test wait
*
* @author Administrator
*
*/
private class ThreadForTestWait extends Thread {
public void run() {
Log.i("ThreadForTestWait", "wait start");
try {
synchronized (this) {
wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("ThreadForTestWait", "wait end");
}
}
}[/mw_shl_code]
我们来看看这个例子怎么样,这里主要用到了view.View.OnClickListener;监听,android.widget.Button按钮,我们定义一个Button是开始,一个Button定义为停止。这样我们就可以给按钮加上一个监听,在监听里定义当点击按钮时,就可以
停止AsyncTask和Thread,这个方法我个人感觉非常的好。这个主要是加了一个sleep(30000);这样的话,我们就有时间来判断一下是否应该怎么样做。