You have to use Asynctask to accomplish the behavior that you mentioned.
See the sample code here--
class ProcessData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = ProgressDialog.show(Questionnaire.this,
"Processing","Downloading survey...");
}
protected String doInBackground(String... args)
{
// delete database values
deleteDatabaseValues();
Log.e("inside","dialogbox");
downloadDatabase();
dialog.dismiss();
return ""; //or something you want to return
}
protected void onPostExecute(String result)
{
//Do anything with the result
dialog.dismiss();
}
Remember to call new ProcessData().execute(<your params>)
to call the above Asynctask.