如何设置AsyncTask执行的超时时间? [英] How to set time out for AsyncTask execution?

查看:664
本文介绍了如何设置AsyncTask执行的超时时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在AsyncTask执行超过1分钟时创建超时.如果时间到了,则应该退出并显示Notification.

I have been trying to create time out while AsyncTask execution more than 1 minute. If the time up, then should exit with Notification.

这是我的代码:

private class GetLongLat extends AsyncTask<Void, Void, Void> {

    @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();
    }

}

这是在doInBackground中调用的用于设置时间的类.

And this is a class called in doInBackground to set the time up.

class TaskKiller extends TimerTask {
 private AsyncTask<?, ?, ?> mTask;

  public TaskKiller(AsyncTask<?, ?, ?> task) {
    this.mTask = task;
  }

  public void run() {
     mTask.cancel(true);
  }
}

但是当我运行代码时,什么也没发生.我的意思是进度对话框总是运行很长时间.

But when i run the code, nothing happen. I mean the progress dialog always run very long time.

编辑 我已经编辑了代码以类似以下方式调用GetLongLat:

EDIT I have edit my code to call GetLongLat something like this:

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);
} 

但是,也不起作用.

推荐答案

我认为您可以使用AsyncTask.get()

I think you can use AsyncTask.get()

GetLongLat n = new GetLongLat();
n.get(30000, TimeUnit.MILLISECONDS);

您将不得不在单独的线程中使用n.get.

you will have to use the n.get in a separate Thread..

另一种方法,但效率不高.

Edited: one more different method but not efficient.,

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

这篇关于如何设置AsyncTask执行的超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆