Chapter 5: AsyncTask and ProgressBar exercises (Android) and androidasynctask
<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 = "start"/> <Button android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/main_stop" android: text = "stop"/> <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; Progress Bar pd; MyTask as; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the data 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);} // click method @ Overridepublic void onClick (View arg0) {// click Start if (button_start.getId () = arg0.getId ()) {as = new MyTask (); // The parameters in execute () can be left blank, or one or more parameters can be passed. // The response parameters and doinbackground## are accepted by the accepted as.exe cute (); // click Close} else if (button_stop.getId () = arg0.getId () {. cancel (true) ;}}// write a class to inherit from AsyncTaskclass MyTask extends AsyncTask <String, Integer, String >{@ Overrid Eprotected void onPreExecute () {// TODO Auto-generated method stubsuper. onPreExecute (); Log. I ("qing", "onPreExecute") ;}@ Overrideprotected String doInBackground (String... arg0) {/************************************** * *** if there is a loop, be sure to write it in try and write sleep at the same time. * otherwise, the AsyncTask cannot be stopped when the cancel () method of AsyncTask is executed, * because the entire AsyncTask is stopped because it is Thread during sleep. sleep (), * If you click "execute cancel ()", an exception is reported. Therefore, the system exits. Otherwise, it will keep repeating. * although the cancel () method will be executed, the AsyncTask will not stop. **************************************** */Try {for (int I = 0; I <= 100; I ++) {Thread. sleep (100); publishProgress (I) ;}} catch (InterruptedException e) {e. printStackTrace ();} return null;} @ Overrideprotected void onProgressUpdate (Integer... values) {// TODO Auto-generated method stubpd. setProgress (values [0]); Log. I ("q", "onProgressUpdate") ;}@ Overrideprotected void onPostExecute (String result) {// TODO Auto-generated method stubToast. makeText (getApplication (), "end of progress", Toast. LENGTH_SHORT ). show (); Log. I ("q", "onPostExecute") ;}@ Overrideprotected void onCancelled () {// TODO Auto-generated method stubsuper. onCancelled (); Log. I ("q", "onCancelled ");}}}
Debugging: