Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I achieved it finally. Note that the AsyncTask cancel(true) method is useless in my scenario because of the recursion. I used what @CommonsWare suggested - used a flag to indicate whether any more tasks should be executed.</p> <pre><code>public class MyActivity extends Activity { /*Flag which indicates whether the execution should be halted or not.*/ private boolean mCancelFlag = false; private AjaxRequestTask mAjaxTask; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(mAjaxTask == null){ mAjaxTask = new AjaxRequestTask(); } mAjaxTask.execute(MY_REST_API_URL); } @Override protected void onResume() { super.onResume(); mCancelFlag = false; /*when we resume, we want the tasks to restart. Unset cancel flag*/ /* If the main task is Finished, create a new task and execute it.*/ if(mAjaxTask == null || mAjaxTask.getStatus().equals(AsyncTask.Status.FINISHED)){ new AjaxRequestTask().execute(TLS_REST_API_URL); } } @Override protected void onPause() { mCancelFlag = true; /*We want the execution to stop on pause. Set the cancel flag to true*/ super.onPause(); } @Override protected void onDestroy() { mCancelFlag = true;/*We want the execution to stop on destroy. Set the cancel flag to true*/ super.onDestroy(); } private void updateReadings(String result) { //Update the UI using the new readings. } class AjaxRequestTask extends AsyncTask&lt;String, Integer, String&gt; { private AjaxRequestTask mChainAjaxRequest; private Timer mTimer; private TimerTask mTimerTask; @Override protected String doInBackground(String... restApiUrl) { //Do AJAX call and get the response return ajaxResponse; } @Override protected void onPostExecute(String result) { Log.d(TAG, "Updating readings"); updateReadings(result); // super.onPostExecute(result); if(mTimer == null){ mTimer = new Timer(); } if(!mCancelFlag){/*Check if the task has been cancelled prior to creating a new TimerTask*/ if(mTimerTask == null){ mTimerTask = new TimerTask() { @Override public void run() { if(!mCancelFlag){/*One additional level of checking*/ if(mChainAjaxRequest == null){ mChainAjaxRequest = new AjaxRequestTask(); } mChainAjaxRequest.execute(MY_REST_API_URL); } } }; } mTimer.schedule(mTimerTask,TIMER_ONE_TIME_EXECUTION_DELAY); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload