Adding android progress dialog in background using AsyncTask, getting FATAL exception

  • An Iam that calls Asynctask from a scheduled service every 10 minutes will be executed.

  • when the service starts, the Run Dialog gets an exception from OnpreExecute .

ERROR :

FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRootImpl.setView(ViewRootImpl.java:594) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) 

EDIT 1: Alarm Manager to call the service every 5 minutes

 /*Alarm manager Service for From Server*/ private void setServerFetch() { // for to Server to GPS PING Intent myIntent1 = new Intent(LoginPage.this, AlarmService.class); pendingintent1 = PendingIntent.getService(LoginPage.this, 1111, myIntent1, 0); AlarmManager alarmManager5 = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(System.currentTimeMillis()); calendar1.add(Calendar.SECOND, 1); alarmManager5.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingintent1); alarmManager5.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 300 * 1000, pendingintent1); } 

Call AsyncTask from Service Onstart

  @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); try { Asynctask_Incident task=new Asynctask_Incident(); task=new(); } catch (Exception e) { e.printStackTrace(); Log.i("PING", "EXCEPTION in reading Data from Web Async task ONstart.!"); } } 

Class Asynctask onStart

 public class Asynctask_Incident extends AsyncTask<String, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { @Override public void run() { if (!pDialog.isShowing()) { pDialog = new ProgressDialog(appContext); pDialog.setCanceledOnTouchOutside(false); pDialog.setCancelable(false); pDialog.setMessage("Please Wait Updating Data From..."); pDialog.show(); } } }); } @Override protected Void doInBackground(String... params) { try { getAPICall(); } catch (Exception e) { e.printStackTrace(); if (pDialog.isShowing()) { pDialog.dismiss(); } } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if (pDialog.isShowing()) { pDialog.dismiss(); } } } 

Help solve this problem.

+7
android android-asynctask service android-pendingintent progressdialog
source share
6 answers

In fact, you cannot start the progress dialog from the service, because it needs an activity context, not an application context, which in your case is null.

More details here: link1 , link2 and link3

If you want to trigger a progress dialog based on the service’s action, you can use the Observer project template, see here .

Update: If your application is running, you can use Handler and run it every 5 minutes.

Here is a complete example:

 public class TestActivity extends AppCompatActivity { private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // new Asynctask_Incident(TestActivity.this).execute("url"); handler.postDelayed(this, 5 * DateUtils.MINUTE_IN_MILLIS); } }, 0); } public class Asynctask_Incident extends AsyncTask<String, Void, Void> { ProgressDialog pDialog; Context appContext; public Asynctask_Incident(Context ctx) { appContext = ctx; } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(appContext); pDialog.setCanceledOnTouchOutside(false); pDialog.setCancelable(false); pDialog.setMessage("Please Wait Updating Data From..."); pDialog.show(); } @Override protected Void doInBackground(String... params) { try { getAPICall(); } catch (Exception e) { e.printStackTrace(); if (pDialog.isShowing()) { pDialog.dismiss(); } } return null; } private void getAPICall() { //5 seconds delay for test, you can put your code here try { Thread.sleep(5 * DateUtils.SECOND_IN_MILLIS); } catch (InterruptedException e) { e.printStackTrace(); } } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if (pDialog.isShowing()) { pDialog.dismiss(); } } } } 
+2
source share

Customize your ProgressDialog .

OnPreExecute ();

 runOnUiThread(new Runnable() { @Override public void run() { if (pDialog == null) { pDialog = new ProgressDialog(appContext); pDialog.setCanceledOnTouchOutside(false); pDialog.setCancelable(false); pDialog.setMessage("Please Wait Updating Data From..."); } pDialog.show(); } }); 

OnPostExecute ();

  pDialog.dismiss(); 
+3
source share

Exception:android.vi‌​ew.WindowManager$BadT‌​okenException: Unable to add window -- token null is not for an application occurs when the context is not alive. There may be another reason for this exception, but the main reason is context. Moreover, if the previously shown dialog is not rejected, an exception may occur.

Please try this code:

 runOnUiThread(new Runnable() { @Override public void run() { if(appContext != null) { // if dialog is already showing, hide it if(pDialog != null && pDialog.isShowing()) { pDialog.dismiss(); } if (pDialog == null) { pDialog = new ProgressDialog(appContext); pDialog.setCanceledOnTouchOutside(false); pDialog.setCancelable(false); pDialog.setMessage("Please Wait Updating Data From..."); } pDialog.show(); } else { Log.e("Error","Context is Null"); } } }); 

You can add an additional check: http://dimitar.me/android-displaying-dialogs-from-background-threads/

+1
source share

You do not need to initialize the dialog in the stream in onPreExecute. Because this method is always called on the user interface thread. By causing a stream, you delay it. Thus, doInbackground may have occurred before the dialog was created.

Also, you should not name anything that changes the user interface in the doItBackground method. Because this method works in a workflow. Any user interface call must be in the main thread. OnPostExecute is called by the main thread. Therefore, place the calls associated with it, but not in doInBackground.

These lines in doInbackground need to be deleted.

 if (pDialog.isShowing()) { pDialog.dismiss(); } 
+1
source share

1) You do not need to configure the ProgressDialog inside Runnable , anything in onPreExecute() and onPostExecute() already running in the user interface thread. Only doInBackground() disables the UI thread.

2) Put the AsyncTask class in MainActivity , call it from MainActivity , not from Service . Call AsyncTask from MainActivity as follows:

 new MyAsyncTask(MainActivity.this).execute(""); 

3) Finally, put this constructor in the AsyncTask class:

 public MyAsyncTask(Context context) { appContext = context; } 
+1
source share

It seems that your context does not have the right set of resources. Make sure you use the correct context.

 Context context = this; ProgressDialog progressDialog = new ProgressDialog(context); progressDialog.show(); 

where "this" is the context of the AppCompatActivity or Activity

0
source share

All Articles