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();
source share