Android ProgressDialog with streaming issue

I am having a problem using ProgressDialog during process execution. I tried every wrong way and looked at numerous websites that offered examples of what I'm trying to do, however I still run into the problem that the thread is working before the ProgressDialog ever appears. Here is my last attempt:

new Thread(new Runnable() { public void run() { dialog = new ProgressDialog(EPD.this); dialog.setMessage("Loading. Please Wait..."); dialog.show(); } }).run(); getMostWanted(); 

In addition to trying this method, I also tried to create a new theme in getMostWanted (), but I still have the same result. It pauses for ~ 4 or 5 seconds while getMostWanted () and there is no dialog box.

Thanks in advance for your help.

+4
source share
4 answers

If you are in the main thread, you should use it to display the ProgressDialog and allocate another thread for getMostWanted() . If you want the end of getMostWanted() reject 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 done in the background thread in doInBackground() , and then after you are done, you can cancel the dialog in the main thread in onPostExecute() .

Now you can use:

new GetMostWanted(dialog).execute();

+2
source

@Amin is correct, that AsyncTask is a good way to approach the problem, although its implementation does not quite meet your needs. You must create your dialog box in onPreExecute() and delete it in onPostExecute() .

The problem you are facing is that creating a new Thread and then calling run will start your thread in the UI thread. Your call to getMostWanted() also executes in the user interface thread and blocks the creation of the dialog.

Therefore, your options should use AsyncTask , as others have suggested, or use Thread and Handler , where Handler performs user interface updates.

+1
source

There is no guarantee when performing a thread. I suggest you use AsycTask instead. Here is an example. Then you can execute it and update your progress bar.

  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"); } } 
0
source

Jay ... IF you want to use threads and handlers. Put the time-consuming task in the runnable method. Create a thread containing an “executable”, and call the start of the thread so that a time-consuming task runs in a separate thread. You should be able to run the dialog in the user interface thread. You should be able to cancel the dialog in the handler that is associated with the user interface thread when the background thread terminates and notifies the handler. Here is the code that uses streams. This fragment starts an 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 are waiting for the thread to complete:

 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
source

Source: https://habr.com/ru/post/1341362/


All Articles