android - Showing ProgressDialog while a Service is being started -


i'm having serious problems when showing progressdialog while service getting ready... service takes time ready it's bit heavy, want show progressdialog meanwhile it's started.

the thing shows progressdialog right before next activity starts... don't find is...

package org.pfc;  import android.app.activity; import android.app.progressdialog; import android.content.broadcastreceiver; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.content.serviceconnection; import android.os.bundle; import android.os.ibinder; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button;   public class connectactivity extends activity {      // fields------------------------------------------------------------------      protected localservice msmeppservice;     private progressdialog progressdialog;      private thread tt;      private serviceconnection mconnection = new serviceconnection() {         public void onserviceconnected(componentname classname, ibinder service) {             // gets object interact service             msmeppservice = ((localservice.localbinder) service).getservice();         }          public void onservicedisconnected(componentname classname) {             // called when connection service has been             // unexpectedly disconnected -- is, process crashed.             // because running in our same process, should never             // see happen.             msmeppservice = null;         }     };      // getting confirmation service     private broadcastreceiver servicereceiver = new broadcastreceiver() {          @override         public void onreceive(context context, intent intent) {             log.i(tag, "receiver onreceive...");              if (progressdialog.isshowing())                 progressdialog.dismiss();              // change activity             intent groupsactivityintent = new intent(connectactivity.this,                     groupsactivity.class);             startactivity(groupsactivityintent);         }     };      // methods ----------------------------------------------------------------      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          if (localservice.isrunning) {             // todo start listactivity             log.i(tag, "starting groupsscreen");              intent = new intent(connectactivity.this, groupsactivity.class);             startactivity(i);         } else {              setcontentview(r.layout.connect_screen);              // add listener button             button buttonconnect = (button) findviewbyid(r.id.button_connect);             buttonconnect.setonclicklistener(new view.onclicklistener() {                  @override                 public void onclick(view v) {                     processthread();                 }             });         }     }       // private methods --------------------------------------------------------      private void processthread() {          progressdialog = progressdialog.show(connectactivity.this, "",                 "loading. please wait...", true, false);          tt = new thread() {             public void run() {                  // register broadcastreceiver know when service finished                 // creation                 connectactivity.this.registerreceiver(servicereceiver,                         new intentfilter(intent.action_view));                  // starts service                 startservice(new intent(connectactivity.this,                         localservice.class));                  log.i(tag, "receiver registered...");             }         };         tt.start();     } } 

the service executes end of onstart method this:

// send broadcast activities take intent = new intent(intent.action_view);     sendorderedbroadcast(i, null); 

so onreceive method runs , go next activity

the problem not running progressdialog in ui thread.

add handler handle messages in ui thread.

private static final int update_started = 0; private static final int update_finished = 1;  private handler handler = new handler(){   @override public void handlemessage(message msg) {     switch (msg.what) {      case update_started:        progressdialog = progressdialog.show(connectactivity.this, "",             "loading. please wait...", true, false);                      break;        case update_finished:        if(progressdialog.isshowing()){          progressdialog.dismiss();            }                  break;     }   } };   private void processthread() {   message m = new message();   m.what = update_started;   handler.sendmessage(m);    //your working code    m = new message();   m.what = update_finished;   handler.sendmessage(m); } 

good luck!


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -