============ Problem Description ============
Recently do a large amount of computing applications, the program in the calculation of the application will be stuck and black screen but will not error, the calculation is completed after the program will continue to run, how to add a loading bar in the calculation process so that the application will not black screen it ...?
============ Solution 1============
Is the message prompting the user to "calculate ..." before displaying the calculation results? Then calculate the result, the hint disappears? In this case you can write a progressdialog dialog box, select the style of the circle ...
============ Solution 2============
Give your calculation code to the service or thread, and the result is returned with a broadcast, the UI displays a prompt when it starts processing, the UI listens to the results, and the UI interface is updated after processing.
============ Solution 3============
The application thread is calculated and the update interface is completed.
============ Solution 4============
Understanding the use of asynchronous thread asynctask should be able to help you
============ Solution 5============
Compute part, put it in the thread, in addition, the UI thread displays ProgressDialog, and after the calculation, the thread closes by handler notification ProgressDialog
============ Solution 6============
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
imageview= (ImageView) Findviewbyid (r.id.picture);
button= (Button) Findviewbyid (R.id.button);
Dialog=new ProgressDialog (this);
Dialog.settitle ("hint");
Dialog.setmessage ("image download");
Dialog.setprogressstyle (progressdialog.style_horizontal);
Dialog.setcancelable (FALSE);
}
public class MyTask extends asynctask<string,integer,bitmap>{
Represents an action before a task action
@Override
protected void OnPreExecute () {
Super.onpreexecute ();
Dialog.show ();
}
@Override
protected void Onprogressupdate (Integer ... values) {
TODO auto-generated Method Stub
Super.onprogressupdate (values);
Dialog.setprogress (Values[0]);
}
Primary execution time-consuming operations
@Override
Protected Bitmap doinbackground (String ... params) {
TODO auto-generated Method Stub
Bytearrayoutputstream outputstream=new Bytearrayoutputstream ();
InputStream Inputstream=null;
Bitmap Bitmap=null;
try {
HttpClient httpclient=new defaulthttpclient ();
HttpGet httpget=new HttpGet (params[0]);
HttpResponse Httpresponse=httpclient.execute (HttpGet);
if (Httpresponse.getstatusline (). Getstatuscode () ==200) {
Inputstream=httpresponse.getentity (). getcontent ();
Long File_length=httpresponse.getentity (). Getcontentlength ();
int len=0;
Byte[] Data=new byte[1024];
int total_length=0;
while ((Len=inputstream.read (data))!=-1) {
Total_length+=len;
int value= (int) ((total_length/(float) file_length) *100);
Publishprogress (value);
Outputstream.write (Data,0,len);
}
Byte[]result=outputstream.tobytearray ();
Bitmap=bitmapfactory.decodebytearray (result, 0, result.length);
}
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}finally{
if (inputstream!=null) {
try {
Inputstream.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
return bitmap;
}
With the new UI interface
@Override
protected void OnPostExecute (Bitmap result) {
TODO auto-generated Method Stub
Super.onpostexecute (result);
Imageview.setimagebitmap (result);
Dialog.dismiss ();
}
}
I this is horizontal, you can choose round, and I this can be downloaded according to the network, show progress. You can change it according to your needs, and put your calculations into the doinbackground.
Android Round loading bar issue