Android Development ProgressDialog Update progress in a standalone thread thread

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.