Analog file copy:
Requirements: To use ProgressDialog and sub-threads to simulate the progress of the copy, after the completion of the main interface prompt copy is completed, respectively, using the normal mode and message mechanism.
Layout file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp2.PractiseActivity6" One android:orientation= "vertical"> A - <Button - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:text= "File copy __ normal way" - Android:onclick= "Bt_kaobei1"/> - <Button + Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:text= "File copy __ message mechanism" A Android:onclick= "Bt_kaobei2"/> at </LinearLayout>
Java class:
1 Packagecom.hanqi.testapp2;2 3 ImportAndroid.app.ProgressDialog;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.os.Message;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.view.View;9 ImportAndroid.widget.Toast;Ten One Public classPractiseActivity6extendsappcompatactivity { A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (R.LAYOUT.ACTIVITY_PRACTISE6); - } - Public voidBt_kaobei1 (View v) + { - FinalProgressDialog ProgressDialog =NewProgressDialog ( This); +Progressdialog.settitle ("File copy"); AProgressdialog.setmessage ("In copy, please wait ..."); at Progressdialog.setprogressstyle (progressdialog.style_horizontal); -Progressdialog.setcancelable (false); - progressdialog.show (); - NewThread () { - @Override - Public voidrun () { in for(inti=0;i<=100;i++) - { to progressdialog.setprogress (i); + Try { -Thread.Sleep (3000); the } * Catch(Exception e) $ {Panax Notoginseng - } the } +Runonuithread (NewRunnable () { A @Override the Public voidrun () { + -Toast.maketext (PractiseActivity6. This, "File copy Complete", Toast.length_short). Show (); $ } $ }); - Progressdialog.dismiss (); - } the }.start (); - }WuyiHandler h =NewHandler () { the @Override - Public voidhandlemessage (Message msg) { Wu Super. Handlemessage (msg); - if(Msg.what ==1) About { $ FinalProgressDialog ProgressDialog =NewProgressDialog (PractiseActivity6. This); -Progressdialog.settitle ("File copy"); -Progressdialog.setmessage ("In copy, please wait ..."); - Progressdialog.setprogressstyle (progressdialog.style_horizontal); AProgressdialog.setcancelable (false); + progressdialog.show (); the NewThread () { - @Override $ Public voidrun () { the for(inti=0;i<=100;i++) the { the progressdialog.setprogress (i); the Try { -Thread.Sleep (3000); in } the Catch(Exception e) the { About the } the } theRunonuithread (NewRunnable () { + @Override - Public voidrun () { the BayiToast.maketext (PractiseActivity6. This, "File copy Complete", Toast.length_short). Show (); the } the }); - Progressdialog.dismiss (); - } the }.start (); the } the } the }; - Public voidBt_kaobei2 (View v) the { the NewThread () { the @Override94 Public voidrun () { theMessage m =Message.obtain (); theM.what = 1; theH.sendemptymessage (1);98 } About }.start (); - }101}
The effect is:
android--Analog File copy