Showing ProgressDialog while a Service is being started

后端 未结 1 911
南旧
南旧 2020-12-16 07:39

I\'m having serious problems when showing a ProgressDialog while a service is getting ready... The service takes time to get ready as it\'s a bit heavy, so I want to show th

相关标签:
1条回答
  • 2020-12-16 08:16

    The problem is that you are not running ProgressDialog in a UI thread.

    Add a handler that will handle messages in your 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!

    0 讨论(0)
提交回复
热议问题