ProgressDialog is similar to the ProgressBar class.
Used to display a procedure that is typically used for time-consuming operations.
Several methods are introduced:
1.setMax ()
Sets the maximum value of the progress bar in the dialog box.
2.setTile ()
Set the caption.
3.setProgressStyl ()
Sets the style of the progress bar in the dialog box. For example: annular and horizontal.
Parameters:
Progressdialog.style_spinner Ring Precision Strip
progressdialog.style_horizontal Horizontal style of progress bar
4.setMessage ()
Sets the contents of the display.
Demo Example:
Used to simulate a time-consuming connection operation.
Figure:
Mainactivity.java
public class Mainactivity extends Activity {button button, button2; ProgressDialog progressdialog;public final static String about_server = "Connect";//information broadcast receive Broadcastreceiver Broadcastreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (Context C, Intent Intent) {String str = Intent . Getstringextra ("DATA"), if (Str.equals ("0")) {progressdialog.setmessage ("Start Connection"),} else if (Str.equals ("1")) { Progressdialog.setmessage ("Connection Complete");} else {Progressdialog.dismiss ();}}}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//Registered broadcast monitor Intentfilter intentfilter = new Intentfilter (); Intentfilter.addaction (ABOUT_SERVER ); Registerreceiver (Broadcastreceiver, intentfilter);} public void Click (View v) {progressdialog = new ProgressDialog (mainactivity.this);p Rogressdialog.setprogressstyle ( Progressdialog.style_spinner);p rogressdialog.setmessage ("Ready to establish connection"); Progressdialog.show (); Intent Intent = new Intent (MainActivity.this, Serveractivity.class); StartService (intent);}} Activity_main.xml
<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 " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:onclick= "click" android:text= "tap"/> </LinearLayout>
Serveractivity.java
public class Serveractivity extends Service {Handler Handler = new Handler () {public void Handlemessage (Message msg) {Inte NT Intent = new Intent (mainactivity.about_server), if (Msg.what = = 0) {Intent.putextra ("DATA", "0");} else if (Msg.what = = 1) {Intent.putextra ("Data", "1");} else {Intent.putextra ("Data", "2"); ServerActivity.this.stopSelf ();} Sendbroadcast (intent);}; @Overridepublic void OnCreate () {super.oncreate (); Thread thread = new Thread (new Runnable () {@Overridepublic void Run () {//What to do try {for (int i = 0; i < 3; i++) {thread. Sleep (1000); Message message = new Message (); message.what = i;handler.sendmessage (message);}} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}); Thread.Start ();} @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method Stubreturn null;}}
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.test "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbacku P= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <servi Ce android:name= "com.example.test.ServerActivity" ></service> <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action A Ndroid:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
In doing this instance, encountered an error, a large string of scarlet letter, the most critical sentence:
This message is already on use.Search from the Internet: related to handler send message, messages are already in the message queue, and is being used, and if the same queue to send the same object will be an error. This error is caused by:
Final message message = new Message (); Thread thread = new Thread (new Runnable () {@Overridepublic void Run () {//What to do try {for (int i = 0; i < 3; i++) {thread. Sleep (+); message.what = i;handler.sendmessage (message);}} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}); Thread.Start ();
the same message is used. With the API 19 (4.4) and 4.4 emulator, the error is not displayed, just stuck, using the API 19 (4.4) with the 2.2 simulator on the error.
The use of Android development ProgressDialog