The progress bar in an objective way, let us know what the program is doing, when the program needs time to perform the task, the prompt progress bar friendly told the user that the current task has not been completed, please wait a little, progress bar is often used in app download applications, update applications, load network data, use frequently, Common progress bar has progressdialog, ProgressBar, here as long as the introduction of ProgressDialog.
ProgressDialog read file Progress parsing mainly involves one knowledge:
- Handler message processing mechanism,
- The operation of the file byte stream,
- Use of the ProgressDialog class
1, Handler message processing mechanism
Handler message processing mechanism, receive the message of the substring Cheng, according to the message passing value, perform UI interface update operation, in order to prevent blocking the main thread, the most use is sendemptymessage (int), SendMessage (int), because often need, as long as the So remember the first handler code, no later can be copied over, as follows:
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
switch (msg.what)
{ Case 110://performs UI update operations
Progressvalue + = Msg.arg1 according to what value;
Sb.append (msg.obj);
Mtextview.settext (Sb.tostring ());//textview Display read content
mdialog.setprogress (progressvalue);//Set progress bar Current value
if ( Progressvalue = = 100) {//interpretation read
-end Mdialog.dismiss ();
Toast.maketext (Mainactivity.this, "read complete!") ", Toast.length_short). Show ();
break;
}
}
;
2, the operation of the file byte stream
The content of the input output stream is not as difficult as it might seem. In "Java object-oriented Programming" in the book is divided into: File class, files byte stream, file character stream, buffer flow, file dialog box, random flow, array flow, data flow with progress bar of the input flow, object flow, often forget their corresponding classes, Easy to use and confusing, write a simple example below:
1), File class
File path = Environment.getexternalstoragedirectory ()//Get SDcard root path
file = new file (path +)/ ProgressMonitor.txt ");/read Progressmonitor file under root path
2), File byte throttling
private void ReadFromFile (String path) {FileInputStream fis;
DataInputStream dis;
try {fis = new FileInputStream (path);
dis = new DataInputStream (FIS);
byte b[] = new byte[10];//reads 10 bytes while (readed = Dis.read (b))!=-1) {msg = new message ();
String Str=new string (b,0,readed);
MSG.OBJ=STR;
MSG.ARG1 = readed;
Msg.what = 110;
Handler.sendmessage (msg);
try {thread.sleep (1000);
catch (Exception e) {e.printstacktrace ();
A catch (FileNotFoundException e) {e.printstacktrace ());
catch (IOException e) {e.printstacktrace ();
}finally {try {dis.close ();
Fis.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
}
}
3, the use of ProgressDialog
ProgressDialog inherits Alertdialog,alertdialog Inheritance dialog, the Common properties are:
Mdialog.setprogressstyle (progressdialog.style_horizontal);/progress bar style
Mdialog.setmax (100);//progress bar Max value
Mdialog.settitle ("System hint"),//progress bar title
Mdialog.seticon (R.drawable.ic_launcher),//progress bar icon
Mdialog.setmessage (" Reading files, please wait ... //progress bar prompt information
mdialog.setprogress (progressvalue),//progress bar Current value
Mdialog.dismiss ()//progress bar disappears
Complete code:
private static final int style_horizontal =;
private static final int style_spinner =;
private void Progressdialogstyle (int id) {
switch (ID) {case
Style_spinner:
mdialog = new ProgressDialog ( this, progressdialog.style_spinner);
break;
Case Style_horizontal:
mdialog = new ProgressDialog (this);
Mdialog.setprogressstyle (progressdialog.style_horizontal);
break;
Default: Break
;
}
Mdialog.setmax (m);
Mdialog.settitle ("system hint");
Mdialog.seticon (r.drawable.ic_launcher);
Mdialog.setmessage ("Reading the file, please wait ...") ");
}
The ProgressDialog class can make a common progress bar effect, and if you need more progress bar styles, you can override ProgressDialog provided methods or inherit Alertdialog custom styles.
The above is the entire content of this article, I hope to help you learn.