AsyncTaskTest
来源:互联网 发布:单片机仿真器 编辑:程序博客网 时间:2024/05/17 02:35
class Log { static void i(String logMe) { android.util.Log.i("hyz", logMe); }}
package hyz.com;import zte.com.cn.R;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;import hyz.com.Log;
public class AsyncTaskTest01 extends Activity implements OnClickListener{ private Button bt1,bt2,bt3; private TextView tv; private ProgressBar pb; private Sleep sp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt1 = (Button)findViewById(R.id.bt1); bt1.setOnClickListener(this); bt2 = (Button)findViewById(R.id.bt2); bt2.setOnClickListener(this); bt3 = (Button)findViewById(R.id.bt3); bt3.setOnClickListener(this); tv = (TextView)findViewById(R.id.tv); pb = (ProgressBar)findViewById(R.id.progress_bar); } @Override public void onClick(View v) { //按bt1表示在同一线程下延时5秒,再按bt3不会立即响应。由于是同一线程,Activity休眠期点击不能立即输出。。。 if(v.equals(bt1)) { sp = new Sleep(); sp.sleep5(); } //按bt2表示在不同线程下延时5秒,再按bt3会立即响应,由于是异步线程,Activity休眠期点击也能立即输出。。。 if(v.equals(bt2)) { //execute()方法启动了一个新线程,新线程执行的是doInBackground()方法,1000、3、10、5是doInBackground()的参数值 new FirstAsyncTask().execute(1000,3,10,5); } if(v.equals(bt3)) { Log.i("在同一线程和不同线程下,Activity休眠期点击输出日志情况。。。"); } } //异步操作类 //第一个Integer定义的是:doInBackground()参数类型 //第二个Integer定义的是:onProgressUpdate()参数类型 //String定义的是doInBackground()返回值类型和onPostExecute()参数类型 class FirstAsyncTask extends AsyncTask<Integer, Integer, String> { public FirstAsyncTask( ) { super(); } //还是在原来UI线程中调用,执行execute()先执行onPreExecute(),再执行doInBackground(), //doInBackground()结束后返回并告诉UI线程,UI线程开始执行onPostExecute() @Override protected void onPreExecute() { tv.setText("开始执行异步操作。。。"); super.onPreExecute(); } //在新线程中操作,arg0是变长数组,通过execute()方法传值 protected String doInBackground(Integer... arg0) { sp = new Sleep(); sp.sleep5(); int i = 10; for(;i<=100;i=i+10) { /* * 每次调用这个方法,都会触发onProgressUpdate()执行, * i即为onProgressUpdate(Integer... values)参数值, * 有了此方法就可以在另外一个线程中操作原来UI线程了 */ publishProgress(i); sp.sleep1(); } return "异步操作执行结束。。。"+arg0[3]; } //doInBackground()结束后执行,还是在原来UI线程中调用,result为doInBackground()的返回值 @Override protected void onPostExecute(String result) { tv.setText(result); } //配合doInBackground()里的publishProgress()使用,values为publishProgress(i)里的i值 @Override protected void onProgressUpdate(Integer... values) { pb.setProgress(values[0]); super.onProgressUpdate(values); } } class Sleep { protected void sleep5() { try { Thread.sleep(5000); } catch(InterruptedException e) { e.printStackTrace(); } } protected void sleep1() { try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } } }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt1" android:text="ctivity在同一线程中休眠5s" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/bt2" android:text="Activity在新线程中休眠5s" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/bt3" android:text="同一线程和不同线程休眠期输出日志情况" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ProgressBarandroid:id="@+id/progress_bar"android:layout_width="fill_parent" android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"/></LinearLayout>