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(); } } } }
Abdulhamid dhaiban
source share