Progress dialog inside async task not working - android

The progress dialog does not show up when the file is downloading in background. After uploading the file to the database the activity remains as it is as if nothing is happening! A progress dialog is necessary while the file is uploading.
public class MainActivity extends AppCompatActivity {
ProgressDialog pd;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null){
pd=new ProgressDialog(this);
Common common=new Common();
MyTask myTask=common.new MyTask(pd);
myTask.execute();
}
}
Asynctask:
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
public MyTask(ProgressDialog pd) {
this.pd = pd;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading . . .");
pd.show();
pd.setCancelable(false);
}
#Override
protected Void doInBackground(Void... voids) {
//download file
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pd.dismiss();
}
}
I searched all the answer of stack overflow but nothing helped me! Any help is appreciated.

Progress Dialog is deprecated anyway, may be you can use progress bar in your xml with visibility:gone, and when you are about to start download change its visilbility to visible and when it is finished set again it go gone

Related

Showing activity indicator in android

I am new to android.I need to show an activity indicator while synchronizing.On clicking a button,i am redirecting to a new activity.In this new activity i am synchronizing.I need to show an activity indicator on button click till it is synchronized.
Synchronizing is effectively a network task And you have to do this on Background ( Async Task) So you can call an AsyncTask in your new Activity
private class SyncOperation extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
// Synchronize code here
return null;
}
#Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
protected void onPreExecute() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Synchronizing, please wait...");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
}
}
}
Now in OnCreate() of new Activity
SyncOperation syncTask=new SyncOperation();
syncTask.execute();
It will show a loader like

Android: Progress dialog not shown at 1st time (using Thread or AsyncTask for longDuration operation)

I have problem in my app with progress dialog in AsyncTask:
my AsyncTask is doing some geting data from internet in doInBackGround(), I am starting progress dialog in onPreExecute(), finish dialog in onPostExecute().
I execute this asyncTask when click on Button. 1st time When I click this button (long operation takes place) progress dialog is not shown. But next times Progress dialog is shown normaly.
What can be the problem that 1st time of running AsyncTask, progress dialog isnt shown ?
this is onClick method:
public void onClickFind(View view){
new Task_Search().execute();
}
This is task Task_Search:
private class Task_Search extends AsyncTask<Void, Void, Void> {
#SuppressWarnings("deprecation")
protected void onPreExecute() {
showDialog(DIALOG_TASKING);
}
protected Void doInBackground(Void... unused) {
//some geting of web content here
}
#SuppressWarnings("deprecation")
protected void onPostExecute(Void unused) {
dismissDialog(DIALOG_TASKING);
}
}
progress dialog defined in MainActivity:
#SuppressWarnings("deprecation")
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TASKING:
mSearchDialog = new ProgressDialog(this);
mSearchDialog.setMessage("Searching for field number..");
mSearchDialog.setCancelable(true);
return mSearchDialog;
}
return super.onCreateDialog(id);
}
Before of using AsyncTask I used separate Thread for downloading data instead: in onClick, I started first progressDialog then Thread, when Thread finished also progressDialog was stoped,
Behaviour was same:
1st time of click on Button dialog wasnt shown, next times its shown.
Anybody who can help ?
I cant see you calling show() on the dialog.
Replace
#SuppressWarnings("deprecation")
protected void onPreExecute() {
showDialog(DIALOG_TASKING);
}
with
#SuppressWarnings("deprecation")
protected void onPreExecute() {
showDialog(DIALOG_TASKING).show();
}
Do not call showDialog insode onPreExecute().
AsyncTask provides publishProgress() method which can be invoked from doInBackground() to publish updates on the UI thread.
Okay,
I created in MainActivity my own methods:
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Searching for field number..");
mProgressDialog.show();
return;
}else{
mProgressDialog.setMessage("Searching for field number..");
mProgressDialog.show();
return;
}
}
also:
public void closeProgressDialog() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
return;
}
I changed AsyncTask following:
private class Task_Search extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
// showDialog(DIALOG_TASKING);
showProgressDialog();
}
protected Void doInBackground(Void... unused) {
//Some long duration stuff here
}
protected void onPostExecute(Void unused) {
//dismissDialog(DIALOG_TASKING);
closeProgressDialog();
}
}
mProgressDialog is a private member of MainActivity
private ProgressDialog mProgressDialog;
Situation is same: wheck onClick on button: 1st time ProgressDialog stuck, next times it works perfect

implementing progress dialog in which the no of images to be downloaded in more than 1

I want to show progress dialog at the time images are being downloaded from server. i am able to download the images and implement the progress dialog but my problem is that the progress dialog doesnot get dismissed and it only shows the last downloaded image.
I am using the following code for that :
public class BackgroundAsyncTask extends AsyncTask<String, Integer, Bitmap> {
int myProgress;
#Override
protected void onPostExecute(Bitmap result) {
image.setVisibility(View.VISIBLE);
image.setImageBitmap(result);
if(dialog.isShowing())
{
dialog.dismiss();
}
return;
}
protected void onPreExecute() {
dialog = ProgressDialog.show(ProfilePageNormalUser.this,
"Loading...", "Please wait...");
}
#Override
protected Bitmap doInBackground(String... paths) {
return DownloadFile(imageUrl);
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}
}
and the following for calling these:
new BackgroundAsyncTask().execute(imageUrl);
Can anyone tall me what could be the problem
Thanks
You have to call publishProgress() after each image. publishProgress() will then call onProgressUpdate() on the UI thread.

AsyncTask not working with ProgressDialog in android

i am downloading data from website using asynctask and
my code for async task is below
public class getSyncTaskInBackground extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
getSynchronizeTask();
return null;
}
#Override
protected void onPostExecute(Void result) {
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
displaySynchronizetask();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(synchronize.this, "Tasks are synchroning...", "Please wait...");
super.onPreExecute();
}
#Override
protected void onCancelled() {
super.onCancelled();
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
this thread takes more time to complete task
so if i want to cancel this thread in between of process then i had write this code of back button pressed
or if progressbar is already dismiss then i want to close activity by calling finish();
my code for back button is as below
#Override
public void onBackPressed() {
syncThread.cancel(true); //syncThread is the object of getSyncTaskInBackground
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
} else {
finish();
}
}
Now when i pressed back button then the progressdialog is not dismiss
is any mistake in mycode ? is any way to complete my need ?
please help me
Hi
You might want to call
setCancelable with true on your progressDialog instance :
http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)
If you want an event on the cancelation of your progressDialog
you can set an onCancelListener
http://developer.android.com/reference/android/app/Dialog.html#setOnCancelListener(android.content.DialogInterface.OnCancelListener)
hope this helps

ProgressDialog won't show, even in onPreExecute of AsyncTask

In my class, Main extends Activity, I've this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ...
case CREDENTIAL_VIEW:
new SetStatusProgressBar(this).execute();
And there is this nested class:
private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Main ctx;
public SetStatusProgressBar(Main ctx) {
this.ctx = ctx;
dialog = new ProgressDialog(ctx);
}
// progress dialog to show user that contacting server.
protected void onPreExecute() {
this.dialog = ProgressDialog.show(ctx, null,
"Refreshing data from server...", true, false);
}
#Override
protected void onPostExecute(final Boolean success) {
//...
//statements that refresh UI
//...
if (dialog.isShowing()) {
dialog.dismiss();
timerProgressBarStop();
}
}
protected Boolean doInBackground(final String... args) {
//...
//statements to download data from server
//...
return true;
}
}
In the Main class I open a second Activity, in this way:
Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);
That second Activity returns to the Main activity in this way:
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
I don't understand why when I navigate from the second Activity to the Main, the ProgressDialog will show ONLY AFTER that the UI refreshes... In this way the Progress Dialog stays on the screen only for half second... and then hides! :( I'd like to see the ProgressDialog on top during all the download time!
Help, please.
Thank you all
Ok, first of all use getApplicationContext() instead of ctx variable. Using 'this' is not for good memory citizens.
Try updating progressDialog in onProgressUpdate(...) method.

Categories

Resources