title: How to set time out for AsyncTask execution?
body: <p>I have been trying to create time out while AsyncTask execution more than 1 minute. If the time up, then should exit with Notification.</p>
  
  <p>This is my code:</p>
  
  <pre><code>private class GetLongLat extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          longlatDialog = new ProgressDialog(MainActivity.this);
          longlatDialog.setMessage("Fetching Data. Please wait..");
          longlatDialog.setCancelable(false);
          longlatDialog.show();
  
      }
  
      @Override
      protected Void doInBackground(Void... arg0) {
  
           GPSTracker gpsTracker;
  
           //This is the timer to set time out
           Timer timer = new Timer();
           timer.schedule(new TaskKiller(this), 3000);
           timer.cancel();
  
           do{
               gpsTracker = new GPSTracker(MainActivity.this);
               gpsTracker.getLocation();
  
           }while(!String.valueOf(gpsTracker.latitude).equals("0.0"));
  
          return null;
      }
  
      protected void onCancelled() {
       // do something, inform user etc.
          Toast.makeText(getApplicationContext(), "Failed getting long lat. Please check your internet connection", Toast.LENGTH_LONG).show();
          System.exit(1);
      }
  
      @Override
      protected void onPostExecute(Void result) {
          super.onPostExecute(result);
          if (longlatDialog.isShowing())
              longlatDialog.dismiss();
      }
  
  }
  </code></pre>
  
  <p>And this is a class called in doInBackground to set the time up.</p>
  
  <pre><code>class TaskKiller extends TimerTask {
   private AsyncTask&lt;?, ?, ?&gt; mTask;
  
    public TaskKiller(AsyncTask&lt;?, ?, ?&gt; task) {
      this.mTask = task;
    }
  
    public void run() {
       mTask.cancel(true);
    }
  }
  </code></pre>
  
  <p>But when i run the code, nothing happen. I mean the progress dialog always run very long time.</p>
  
  <p><strong>EDIT</strong>
  I have edit my code to call GetLongLat something like this:</p>
  
  <pre><code>GetLongLat n = new GetLongLat();
  n.execute();
  try {
  
      n.get(3000, TimeUnit.MILLISECONDS);
  } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (TimeoutException e) {
      // TODO Auto-generated catch block
      Toast.makeText(getApplicationContext(), "Failed getting long lat. Please check your internet connection", Toast.LENGTH_LONG).show();
      System.exit(1);
  } 
  </code></pre>
  
  <p>But also, doesn't work.</p>