title: Android - Setting a Timeout for an AsyncTask?
body: <p>I have an <code>AsyncTask</code> class that I execute that downloads a big list of data from a website.</p>
  
  <p>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 <code>AsyncTask</code> timeout after a period of time. My first approach to this is like so:</p>
  
  <pre><code>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 );
  </code></pre>
  
  <p>After starting the <code>AsyncTask</code>, a new handler is started that will cancel the <code>AsyncTask</code> after 30 seconds if it's still running.</p>
  
  <p>Is this a good approach? Or is there something built into <code>AsyncTask</code> that is better suited for this purpose?</p>