第五章,AsyncTask和ProgressBar的练习(Android)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_start" android:text="開始" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_stop" android:text="停止" /> <ProgressBar android:id="@+id/main_progress" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> </LinearLayout>
package com.example.demo07; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { Button button_start, button_stop; ProgressBar pd; MyTask as; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据 init(); } private void init() { button_start = (Button) this.findViewById(R.id.main_start); button_stop = (Button) this.findViewById(R.id.main_stop); pd = (ProgressBar) this.findViewById(R.id.main_progress); button_start.setOnClickListener(this); button_stop.setOnClickListener(this); } // 点击方法 @Override public void onClick(View arg0) { // 点击了開始 if (button_start.getId() == arg0.getId()) { as = new MyTask(); // execute()中的參数能够不填。也能够传一个或多个。 // 參数类型与doInBackground方法參数必须一致,由于接受參数就是这种方法 as.execute(); // 点击了关闭 } else if (button_stop.getId() == arg0.getId()) { as.cancel(true); } } // 写一个类继承AsyncTask class MyTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); Log.i("qing", "onPreExecute"); } @Override protected String doInBackground(String... arg0) { /***************************************** * 这里假设有循环,一定要写在try里面,同一时候一定要写休眠sleep, * 不然AsyncTask的cancel()方法运行时此AsyncTask无法停止, * 由于这整个AsyncTask停止时由于它在休眠时Thread.sleep(), * 点击了运行cancel()会报异常,于是退出。不然就会一直循环, * 尽管会运行cancel()方法。但这个AsyncTask不会停止。 *****************************************/ try { for (int i = 0; i <= 100; i++) { Thread.sleep(100); publishProgress(i); } } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub pd.setProgress(values[0]); Log.i("q", "onProgressUpdate"); } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub Toast.makeText(getApplication(), "进度结束", Toast.LENGTH_SHORT).show(); Log.i("q", "onPostExecute"); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); Log.i("q", "onCancelled"); } } }
调试截图: