Android - Setting a Timeout for an AsyncTask?

JavaAndroidAndroid Asynctask

Java Problem Overview


I have an AsyncTask class that I execute that downloads a big list of data from a website.

In the case that the end user has a very slow or spotty data connection at the time of use, I'd like to make the AsyncTask timeout after a period of time. My first approach to this is like so:

MyDownloader downloader = new MyDownloader();
downloader.execute();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
  @Override
  public void run() {
      if ( downloader.getStatus() == AsyncTask.Status.RUNNING )
          downloader.cancel(true);
  }
}, 30000 );

After starting the AsyncTask, a new handler is started that will cancel the AsyncTask after 30 seconds if it's still running.

Is this a good approach? Or is there something built into AsyncTask that is better suited for this purpose?

Java Solutions


Solution 1 - Java

Yes, there is AsyncTask.get()

myDownloader.get(30000, TimeUnit.MILLISECONDS);

Note that by calling this in main thread (AKA. UI thread) will block execution, You probably need call it in a separate thread.

Solution 2 - Java

Use CountDownTimer Class in side the extended class for AsyncTask in the onPreExecute() method:

Main advantage, the Async monitoring done internally in the class.

public class YouExtendedClass extends AsyncTask<String,Integer,String> {
...
public YouExtendedClass asyncObject;   // as CountDownTimer has similar method -> to prevent shadowing
...
@Override
protected void onPreExecute() {
	asyncObject = this;
	new CountDownTimer(7000, 7000) {
		public void onTick(long millisUntilFinished) {
			// You can monitor the progress here as well by changing the onTick() time
		}
		public void onFinish() {
			// stop async task if not in progress
			if (asyncObject.getStatus() == AsyncTask.Status.RUNNING) {
				asyncObject.cancel(false);
                // Add any specific task you wish to do as your extended class variable works here as well.
			}
		}
	}.start();
...

change CountDownTimer(7000, 7000) -> CountDownTimer(7000, 1000) for example and it will call onTick() 6 times before calling onFinish(). This is good if you want to add some monitoring.

Thanks for all the good advice I got in this page :-)

Solution 3 - Java

In the case, your downloader is based upon an for an URL connection, you have a number of parameters that could help you to define a timeout without complex code:

  HttpURLConnection urlc = (HttpURLConnection) url.openConnection();

  urlc.setConnectTimeout(15000);

  urlc.setReadTimeout(15000);

If you just bring this code into your async task, it is ok.

'Read Timeout' is to test a bad network all along the transfer.

'Connection Timeout' is only called at the beginning to test if the server is up or not.

Solution 4 - Java

I don't think there's anything like that built into AsyncTask. Your approach seems to be a good one. Just be sure to periodically check the value of isCancelled() in your AsyncTask's doInBackground method to end this method once the UI thread cancels it.

If you want to avoid using the handler for some reason, you could check System.currentTimeMillis periodically within your AsyncTask and exit on timeout, although I like your solution better since it can actually interrupt the thread.

Solution 5 - Java

         Context mContext;

         @Override
         protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
                                    mContext = this;

            //async task
            final RunTask tsk = new RunTask ();	
	        tsk.execute();
		
            //setting timeout thread for async task
	        Thread thread1 = new Thread(){
	        public void run(){
	            try {
	            	tsk.get(30000, TimeUnit.MILLISECONDS);  //set time in milisecond(in this timeout is 30 seconds
	                
	            } catch (Exception e) {
	                tsk.cancel(true);   		                
	                ((Activity) mContext).runOnUiThread(new Runnable()
					{
						 @SuppressLint("ShowToast")
						public void run()
						 {
							Toast.makeText(mContext, "Time Out.", Toast.LENGTH_LONG).show();
							finish(); //will close the current activity comment if you don't want to close current activity.								
						 }
					});
	            }
	        }
	    };
	    thread1.start();
                               
         }

Solution 6 - Java

You can put one more condition to make cancellation more robust. e.g.,

 if (downloader.getStatus() == AsyncTask.Status.RUNNING || downloader.getStatus() == AsyncTask.Status.PENDING)
     downloader.cancel(true);

Solution 7 - Java

Inspiring from question I have written a method which do some background task via AsyncTask and if processing takes more then LOADING_TIMEOUT then an alert dialogue to retry will appear.

public void loadData()
    {
        final Load loadUserList=new Load();
        loadUserList.execute();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (loadUserList.getStatus() == AsyncTask.Status.RUNNING) {
                    loadUserList.cancel(true);
                    pDialog.cancel();
                    new AlertDialog.Builder(UserList.this)
                            .setTitle("Error..!")
                            .setMessage("Sorry you dont have proper net connectivity..!\nCheck your internet settings or retry.")
                            .setCancelable(false)
                            .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    loadData();
                                }
                            })
                            .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                                @Override
      

                      public void onClick(DialogInterface dialogInterface, int i) {
                                System.exit(0);
                            }
                        })
                        .show();
            }
        }
    }, LOADING_TIMEOUT);
    return;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJake WilsonView Question on Stackoverflow
Solution 1 - JavayorkwView Answer on Stackoverflow
Solution 2 - JavaGilcoView Answer on Stackoverflow
Solution 3 - JavaClement SoullardView Answer on Stackoverflow
Solution 4 - Javaaleph_nullView Answer on Stackoverflow
Solution 5 - JavaMrityunjayaView Answer on Stackoverflow
Solution 6 - JavaVinnigView Answer on Stackoverflow
Solution 7 - JavaAbhishekView Answer on Stackoverflow