Simple requirements: Update the Progress dialog box in a work thread ProgressDialog
Problems encountered:
1, create need context, this need to pass in
You cannot create a progressdialog in 2,thread, or you need to create a looper
3, update progressdialog directly in thread No, throw exception, not create thread view does not allow updates
Create handler outside 4,thread, then post in the ready, and not update.
Solution:
1, create ProgressDialog, create handler at the same time, Update dialog box with callback handlemessage
After calling SendMessage in 2,thread, you also need to call Thread.yield () or Thread.Sleep (), or else it will not be updated.
Example code:
FinalProgressDialog pgdlg=NewProgressDialog (Mcontext); Pgdlg.settitle ("Testing");p gdlg.setmessage (""); Pgdlg.setprogressstyle (progressdialog.style_horizontal); Pgdlg.setindeterminate (false); Pgdlg.setmax (+);p gdlg.setprogress (0); Pgdlg.seticon (Android. R.drawable.ic_menu_search); FinalHandler dlghandler=NewHandler (NewHandler.callback () {@Override Public Booleanhandlemessage (Message msg) {String s=(String) msg.obj; Pgdlg.setmessage (s); if(msg.what>=100) {pgdlg.setprogress (100); NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {Pgdlg.dismiss (); } },1000); }Else{pgdlg.setprogress (msg.what); } return true; } }); Pgdlg.show (); NewThread (NewRunnable () {@Override Public voidrun () {bluetoothgattcharacteristic CTC=blehelper.getcharacteristic (samplegattattributes.service_keyservice,samplegattattributes.characteristic_ Keycharacteristic); for(inti=0;i<cmdstotest.length;i++) {String cmd=Cmdstotest[i]; FinalString Msg=string.format ("command>:%s", CMD); APPUTILS.LOGD (msg); Obdcommand OC=Newobdcommand (cmd, cmd); FinalObdcommandjob job =Newobdcommandjob (OC); Commandexecutor Executor=NewBlecommandexecutor (BLEHELPER,CTC, Job). Setlocker (Mjoblock, mjobcondition); Try{mjoblock.lock (); NewThread (Executor). Start ();Mjobcondition.await (5, Timeunit.seconds); } Catch(Interruptedexception ex) {LOG.E ("", Ex.getmessage ()); Job.setstate (Obdcommandjobstate.execution_error); } finally{mjoblock.unlock (); Blehelper.resumeio (); } if(Job.getstate ()! =obdcommandjobstate.finished) {errormsg= String.Format ("Test command%s failed!"), Job.getcommand (). GetCommand ()); if(Resultlistener! =NULL) {Resultlistener.ontestdone (false, errormsg); Message.obtain (Dlghandler, - , errormsg). Sendtotarget (); Thread.yield ();
break; } } Final intprogress = I*100/cmdstotest.length; Message.obtain (dlghandler,progress,msg + "\NRESULT>:" +job.getcommand (). Getrawdata ()). Sendtotarget (); //pgdlg.setmessage (msg); Throw exception when Access direct here//Pgdlg.incrementprogressby (progress); Thread.yield ();//must yield to let message processed } if(Resultlistener! =NULL) {Resultlistener.ontestdone (true, ""); Message.obtain (Dlghandler, "Done" ). Sendtotarget (); Thread.yield (); } blehelper.close (); }}). Start ();
Android Development ProgressDialog Update progress in a standalone thread thread