开发者

Android ProgressDialog with threading problem

开发者 https://www.devze.com 2023-02-13 06:54 出处:网络
I am running into a problem using the ProgressDialog while a process is running.I have tried every incorrect way possible and have looked at numerous websites which offered examples of what I am tryin

I am running into a problem using the ProgressDialog while a process is running. I have tried every incorrect way possible and have looked at numerous websites which offered examples of what I am trying to do however, I am still running into the problem that the thread is running before the ProgressDialog ever comes up. Here is my latest attempt at this:

new Thread(new Runnable() {
     public void run() {
        dialog = new ProgressD开发者_Python百科ialog(EPD.this);
        dialog.setMessage("Loading. Please Wait...");
        dialog.show();         
                    }
 }).run();
 getMostWanted();                       

In addition to trying this way, I have also attempted to a new Thread in getMostWanted() however I am still having the same result. It pauses for ~4 or 5 seconds while getMostWanted() and no dialog box.

Thanks in advance for the help.


If you're on the main thread, you should use that to display the ProgressDialog and spin off another thread for getMostWanted(). Assuming you want the ending of getMostWanted() to dismiss the dialog, you should look at AsyncTask:

private class GetMostWanted extends AsyncTask<Void, Void, Void> {
     private final ProgressDialog dialog;

     public GetMostWanted() {
          dialog = new ProgressDialog(EPD.this);
          dialog.setMessage("Loading. Please Wait...");
     }

     protected void onPreExecute() {
         dialog.show();
     }

     protected void doInBackground(Void... unused) {
         getMostWanted();
     }

     protected void onPostExecute(Void unused) {
         dialog.dismiss();
     }
 }

That way your processing is performed on a background thread in doInBackground() and then after you're done you can dismiss the dialog on the main thread in onPostExecute().

Now you can use:

new GetMostWanted(dialog).execute();


@Amin is correct that a good way to approach the problem is with an AsyncTask, although his implementation doesn't quite fit your needs. You would create your dialog in onPreExecute(), and remove it in onPostExecute().

The issue you are having is that creating a new Thread then calling run will just run your thread on the UI thread. Your call to getMostWanted() also executes in the UI thread, and blocks the creation of the dialog.

So your options are to use an AsyncTask as others have suggested, or to use a Thread and Handler, where the Handler performs the UI updates.

  • AsyncTask
  • Painless threading
  • Threading
  • Designing for responsiveness
  • Thread documentation
  • Handler documentation


There is no guarantee when the thread is executed. I suggest you use an AsycTask instead. Here is an example. Then you can execute it and update your progressbar.

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }


Jay... IF you want to use threads and handlers. Place the time intensive task into a runnable method. Create the thread "containing" the runnable object and call thread start so that the time intensive task is running on a separate thread. You should be able to launch the dialog in the UI thread. You should be able to dismiss the dialog in the handler, which is bound to the UI thread, when the background thread completes and notifies the handler. Here is some code using threads. This snippet launches a time intensive process in a new thread.

buttonConfuseText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String inString= editTextPlainText.getText().toString();
        Thread thread= new Thread( new Runnable() {
            public void run() {
                String outString= encrypt(password,inString); //<== time intensive task here
                Message msg= Message.obtain();
                Bundle b= new Bundle();
                b.putString("encryptedText",outString);
                msg.setData(b);
                handler.sendMessage(msg);
                Log.d(TAG,outString);
            }
        });
        thread.setDaemon(true);
        thread.start();
    }
});

You wait for the thread to complete here:

public class ConfuseText extends Activity {
    private Handler handler= new Handler(){
        @Override
        public void  handleMessage(Message msg){
            super.handleMessage(msg);
            ConfuseText.this.onThreadMessage(msg); //<== close dialog here if you wish
        }
    };

public void onThreadMessage(Message msg){
    Bundle b= msg.getData();
    String encryptedText="";
    if (b != null){
        encryptedText= b.getString("encryptedText");
    }
    Log.d(TAG,encryptedText);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消