it's something weird , sometimes when I come to my activiity , it calls a asyncTask , this is the code where i use ProgressDialog
ProgressDialog ringProgressDialog ;
#Override
protected void onPreExecute()
{
super.onPreExecute();
ringProgressDialog= ProgressDialog.show(Myactivity.this, null,"message", true);
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
ringProgressDialog.dismiss();
}
somedays the asynctask get error ,I wanted to post the error but somehow I don't have the error .
What is the problem of this ?Why it sometimes get error and sometimes it works fine ?
I'm sure the problem is from progressDialog .
Thanks
ProgressDialog ringProgressDialog ;
#Override
protected void onPreExecute() {
super.onPreExecute();
ringProgressDialog= ProgressDialog.show(getApplicationContext(), null,"message", true);
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(ringProgressDialog.isShowing())
ringProgressDialog.dismiss();
}
try it the other way
ProgressDialog pDialog;
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Activity.this);
pDialog.setMessage("Fetching News...");
pDialog.setCancelable(false);
pDialog.show();
}
Try this way,hope this will help you to solve your problem.
public void getDataFromServer(final Context context){
new AsyncTask<Void,Void,Void>(){
ProgressDialog ringProgressDialog ;
#Override
protected void onPreExecute() {
super.onPreExecute();
ringProgressDialog= ProgressDialog.show(context, null,"message", true);
}
#Override
protected Void doInBackground(Void... params) {
// write your service call code here
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ringProgressDialog.dismiss();
}
}.execute();
}
ProgressDialog pDialog;
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog("Your Activity");
pDialog.setMessage("Your message");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
}
Related
I have a AsyncTask which will be executed in the onCreate method. However, my ProgressDialog isn't showing up. And from debugging, it is confirmed that the AsyncTask is being executed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifestyle);
context = getApplicationContext();
new testAsync().execute();
}
private class testAsync extends AsyncTask<Void,Void,Void> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
#Override
public void run() {
//delay for 5 seconds
}
}, 5000);
return null;
}
#Override
protected void onPostExecute(Void v) {
pDialog.dismiss();
}
}
Take your pDialog.dismiss(); into the your timer thread.
Reason: onPostExecute() immediately call because on background task is finished.
Its seprate thread which is on delay so cursor move to the onPostExecute()
private class testAsync extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
#Override
public void run() {
//delay for 5 seconds
pDialog.dismiss();
}
}, 5000);
return null;
}
#Override
protected void onPostExecute(Void v) {
// pDialog.dismiss();
}
}
Activity will not show any view until onResume method is called. If your AsyncTask execution completes before onResume call then you will never see the ProgressDialog.
So better call new testAsync().execute(); in onResume
add pDialog.show(); in doInBackground and add new testAsync().execute(); in onResume
Your code is working fine with Thread.sleep(5000); if you want to add delay then use Thread.Sleep(/time in milisec/) instead of Timer.Schedule. Because of Timer.Schedule async task is executing so quickly before showing any dialog.
I have created dialog and it inflates xml which gets updated with the info .But the progress dialog is shown behind the dialog which pops up .How do I show those progressdialog on top of dialog with inflated xml.
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public class loadasync extends AsyncTask<Void, Void, JSONObject> {
ProgressDialog progressDialog ;
#Override
protected JSONObject doInBackground(Void... params) {
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
});
}
#Override
protected void onPostExecute(JSONObject result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
}
}
public void dialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialogxml);
loadasync loadasyncdata=new loadasync();
loadasyncdata.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.show();
}
I cannot see that you are using the setProgressStyle() in your code. That is:
progessDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
for instance.
So in your code try the following:
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
Something to consider : OnPostExecute and onPreExecute are both run on the UI thread, so you can remove the runOnUIThread stuff, just do
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
and respectively in onPostExecute
protected void onPostExecute(JSONObject result) {
progressDialog.dismiss();
}
Should maybe be a comment, but the reputation ...
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
Which is the best way to load the data for a single item (read only) detail activity?
Should i use LoaderManager or directly AsyncTask?
Use an Asyntask for best result:
Public class example extends AsyncTask<Void,Void,Void>
{
private ProgressDialog dialog;
public example(Context context){
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
dialog.setTitle("Demo");
dialog.setMessage("Please Wait..");
dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
#SuppressLint("NewApi")
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
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();
}
}