且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置AsyncTask执行的超时时间?

更新时间:2023-02-26 18:27:15

我认为您可以使用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 );