As others have said, an endless loop without a break condition is not a pleasant user experience. First get an instance for your AsyncTask:
PostTask pt = new PostTask(this); pt.execute();
Try this in doInBackground() :
while(!this.isCancelled()){
If the application is closed by the user, AsyncTask must be stopped in onPause() .
@Override public void onPause(){ pt.cancel(false); }
AsyncTask.cancel (boolean) sets isCancelled() to true , calls the AsyncTask.onCanceled() method instead of onPostExecute() and can be overwritten for your own purpose.
If you do not like this, put your task in the service.
source share