Running a long-term task from AlertDialog (Android)

I have an AlertDialog, and it has a button that, if selected, initiates sending the file to the remote server as follows:

builder.setMessage(text) .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { uploadFile(); }}) 

This works great, the only problem is that uploadFile () can potentially last a long time (from 30 seconds to 2 minutes). I would like to replace AlertDialog with a progress dialog (even vague), but I don’t see how to start one dialog from another?

Can someone offer some advice on how I could do this.

Thanks Jarabek

0
android task alertdialog
source share
2 answers

Here you can use AsyncTask , save the ProgressDialog in PreExecute() and call the method in doingInBackground() and release the ProgressDialog in PostExecute().

 private class MyAsyncTask extends AsyncTask<Void, Void, Void> { ProgressDialog mProgressDialog; @Override protected void onPostExecute(Void result) { mProgressDialog.dismiss(); } @Override protected void onPreExecute() { mProgressDialog = ProgressDialog.show(ActivityName.this, "Loading...", "Data is Loading..."); } @Override protected Void doInBackground(Void... params) { uploadFile(); return null; } } 

Then just call MyAsyncTask using new MyAsyncTask().execute();

+5
source share

The best way to do this is using AsyncTask . There you can make progress and make your long action

+1
source share

All Articles