Progressdialog In Asynctask Which Has A Thread In It
Solution 1:
As per your comment : "@Tarsem I want to show the progress dialog while uploading the image. So when the thread in ImageUploader is alive."
Try following apporach:
classimageuploadextendsAsyncTask<Void, Void, Void> {
Dialog dialog;
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(context, "Uploading Image","Please Wait...");
}
@OverrideprotectedVoiddoInBackground(Void... params) {
// Upload Image HERE returnnull;
}
@OverrideprotectedvoidonPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
// You can Call another AsynTask here (achieve sending email work here)
}
}
Solution 2:
You can show a progress dialog in pre execute, do both uploading the image and sending the email in doInBackground and hide the progress dialog in postExectue.
Uploading the image in preexecute is contrary to what the async task is for... If you perform the uploading in doInBackground there is no need for an extra thread. (That's what the AsyncTask handles for you)
Or you could upload the picture and then (in postexecute) start another task for sending the email although I don't see why you need to do that asynchronous? You could also just send an intent to send the email...?
Solution 3:
Just use AsyncTask
in following manner.
In your main class make ProgressDialog
's Object
.
privateclassUploadTaskextendsAsyncTask<Void, Void, Integer > {
@OverrideprotectedvoidonPreExecute(){
super.onPreExecute();
pd = newProgressDialog(MainActivity.this);
pd.show();
}
protected Integer doInBackground(URL... urls) {
if(isPhotoTaken()){
ImageUploader.uploadFile(getPhotoPath(), "http://www.example.com/upload.php",Step4.this);
}
returnnull;
}
protectedvoidonPostExecute(Integer result) {
pd.dismiss();
}
}
Solution 4:
Async task comes with four methods onPreExecute()
doInBackground()
onPostExecute()
and onUpdateProgress()
, you just need to appropriately implement methods.
You show a progress in onPreExecute()
do the upload and send email in doInBackground()
update in onUpdateProgress()
hide progress on onPostExecute()
.
EDIT
once you do it right you don't need thread anymore, because the main idea of AsyncTask is to make a separate thread/process and update the UI onPreExecute()
onUpdateProgress()
onPostExecute()
runs on UI thread while doInBackground()
fires a new thread, and can update UI by calling publishUpdate from within it, which in turn automatically call onUpdateProgress()
Post a Comment for "Progressdialog In Asynctask Which Has A Thread In It"