Android - Progress Dialog crashes when changing message - android

I'm new to Android and I'm practicing creating a progress dialog. I want to change the message in the dialog every couple of seconds, but my application crashes when I change the message. Any ideas what I may be doing wrong?
private void progressDialogTest(final ArrayList<String> messages)
{
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>()
{
private ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Progress Dialog");
progressDialog.show();
}
#Override
protected Void doInBackground(Void... arg0)
{
try
{
for(int i=0; i<messages.size(); i++)
{
/******** APPLICATION SEEMS TO CRASH AT LINE BELOW ********/
progressDialog.setMessage(messages.get(i));
Thread.sleep(3000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
progressDialog.dismiss();
}
};
task.execute((Void[])null);
}

Move the code to onProgressUpdate instead, eg:
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setMessage(messages.get(progress[0]));
}
#Override
protected void doInBackground(Void... arg0) {
/* ... */
//progressDialog.setMessage(messages.get(i)); Change this line to
publishProgress(i);
/* ... */
}

Related

WindowManager$BadTokenException near dialog.show in AsyncTask android

How to show a dialog box in AsyncTask. Getting BadToketException in dialog.show();
I tried many ways but I could not solve it.
Also tried to pass context to the dialog box in different ways, but it is giving me the same result.
public class RetriveStock extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
message = client.clientReceive(1); // I get data here.
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
#Override
protected void onPostExecute(Void result) {
if (message.contains("AlertExecuted:")) {
final Dialog dialog = new Dialog(CreateAlert.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.display_dialog);// Dialog layout
TextView dialogText = (TextView) dialog.findViewById(R.id.digMsg);
dialogText.setText("Alert Executed!");
Button ok = (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
try {
dialog.show(); //WindowManager$BadTokenException
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Please help.
protected void onPreExecute() {
// TODO Auto-generated method stub
// progressDialog = ProgressDialog.show(this, "", "loading news content");
progressDialog = new ProgressDialog(context , AlertDialog.THEME_HOLO_LIGHT);
progressDialog.setMessage(""+getString(R.string.laodnews));
progressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.animate));
progressDialog.setCancelable(false);
progressDialog.show();
}
start dailoge in pre execute and stop in onpostexecute..
is CreateAlert registered activity in manifest..if not then you have to pass registered activity context

Show progressDialog in Android

I want to show a progressDialog on my page when the user clicks on the button.On click of button i am sorting my results that is a List.Now how can we show a progressDialog on click of a button.Please suggest me
This function i am using to sort that data :
public void sortByDate(View v) {
Collections.sort(tripParseData.getDetails());
setData(tripParseData);
}
After #Monica Suggestion
public void sortByDate(View v) {
new LoadData().execute();
}
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
Collections.sort(tripParseData.getCoroprateBookingDetails());
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
setApprovalDetailsData(tripParseData);
}
}
you have to call progressdiolog.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
here you can see an example:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
#Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
Don't forget to vote me up :)
Paste This Class in ur activity and call new LoadData().execute to start the prgress dialog :
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pg=new ProgressDialog(ChartsActivity.this);
pg=pg.show(ChartsActivity.this, "Loding", "Plz Wait...");
}
#Override
protected Void doInBackground(Void... params) {
sortByDate();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pg.dismiss();
}
}
I have not tested it but I think it can help you.
Declare these two objects in your class and member variables.
Thread thread = null;
ProgressDialog bar = null;
Use this logic on your button click listener it can work in your case. I have not tested it but It will give you Idea how things should work. you can also use handlemessage in place of runonuithread
if (thread == null
|| (thread != null && !thread.isAlive())) {
thread = new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//DISPLAY YOUR PROGRESS BAR HERE.
bar = ProgressDialog.show(getApplicationContext(), "LOADING..", "PLEASE WAIT..");
}
});
// SORT COLLECTION HERE
runOnUiThread(new Runnable() {
#Override
public void run() {
//CLOSE YOUR PROGRESS BAR HERE.
bar.dismiss();
}
});
}
});
thread.start();
}
Hope this will help you.
try to use AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
Sample code:
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
//Show UI
//Start your progress bar
showProgress();
}
#Override
protected Void doInBackground(Void... arg0) {
// do your sorting process
return null;
}
#Override
protected void onPostExecute(Void result) {
//Show UI
//dismiss your progress bar
hideProgress();
}
};
task.execute((Void[])null);
Show and hide progress code
public void showProgress() {
progressDialog = ProgressDialog.show(this, "",
"Loading. Please wait...");
progressDialog.setCancelable(false);
}
public void hideProgress() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}

Adding ProgressDialog as Async Task is slowing down the performance

This is my code for Login.
public void Login_Click(View view) {
HashMap<String, String> op_Config = XMLParser
.parse(LoginActivity.this);
}
It takes around 4 Sec to finish executing. So I added progress dialog like this.
public void Login_Click(View view) {
new IsLogedIn().execute();
}
class IsLogedIn extends AsyncTask<String, String, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
#Override
protected String doInBackground(String... value) {
try
{
HashMap<String,String> op_Config = XMLParser.parse(LoginActivity.this);
finish();
Intent intent=new Intent(LoginActivity.this,MainMenuActivity.class);
startActivity(intent);
}
catch (Exception e) {
}
}
#Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
}
}
But now it is taking more than 1 min to finish.
Try doing like this
class IsLogedIn extends AsyncTask<String, String, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
#Override
protected String doInBackground(String... value) {
try
{
HashMap<String,String> op_Config = XMLParser.parse(LoginActivity.this);
}
catch (Exception e) {
}
}
#Override
protected void onPostExecute(String file_url) {
if(dilog.isShowing()){
dismissDialog(progress_bar_type);
}
finish();
startActivity(new Intent(LoginActivity.this,MainMenuActivity.class));
}
}

Delay in taking screenshots

I have an activity in my app where it takes several screen captures and move to next activity on a button click. I want to show some progress or status to the user while taking screenshot(It takes 5-8 seconds). I tries using AsyncTask with runOnUithread(). But no luck. Even when I wrote the whole code in doInBackground(), I don't know why ProgressDialog starts after taking all the screenshots. What is the better way to do this? Here is my code:
public class TakeScreenShots extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle",
"Loading .....", true);
}
#Override
protected Void doInBackground(Void... params) {
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
takeScreenshots();
}
});
return null;
}
#Override
protected void onPostExecute(Void unused) {
gotoNextActivity();
this.dialog.dismiss();
}
}
No you are doing wrong, instead capturing in runOnUiThread Directly capture it in doInBackground()
Here is the working example
public class backImageProcess extends AsyncTask<Void, Void, Void>
{
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle",
"Loading .....", true);
try {
File mFile= new File(mTempImagename);
if(mFile!=null)
mFile.delete();
} catch (Exception e) {
}
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
mLayoutRoot.setDrawingCacheEnabled(true);
mLayoutRoot.buildDrawingCache();
Bitmap mBitmap= mLayoutRoot.getDrawingCache();
try {
if(mBitmap!=null)
{
FileOutputStream out = new FileOutputStream(mTempImagename);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
}
} catch (Exception e) { }
return null;
}
#Override
protected void onPostExecute(Void result) {
if(this.dialog!=null)
this.dialog.dismiss();
gotoNextActivity();
}
}
I got the answer from an answer in this question. I was setting a drawable to the image view. So, I kept that line on Ui Thread and remaining in doInBackground().

Android: Is not showing ProgressDialog

I have AsynTask and show progress dialoge while it is running. Phone receive incoming call. After call stopped the progress dialoge is not showing but the activity layout is dim in such way if the dialoge is showing. Has you any ideas?
I have somethin like this:
Recovery recovery=new Recovery();
recovery.execute();
And asynktask:
public class Recovery extends AsyncTask<String, Void, Integer>{
#Override
protected Integer doInBackground(String... uri) {
publishProgress();
//some code
return Ret;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
showDialog(RECOVERY);
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
dismissDialog(RECOVERY);
}
}
use this way it works
ProgressDialog dialog = new ProgressDialog(YourActivity.this);
#Override
protected void onPreExecute()
{
this.dialog.setMessage("Loading Please Wait...");
this.dialog.show()
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}

Categories

Resources