2012-10-30 92 views
1

使用Timer通過下面的代碼的AsyncTask與定時器繼續運行

public void toCallAsynchronous() { 
    TimerTask doAsynchronousTask; 
    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 


    doAsynchronousTask = new TimerTask() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      handler.post(new Runnable() { 
       public void run() { 

        try { 
         if(mLoggedIn) 
         { 
          DownloadRandomPicture download = new DownloadRandomPicture(this, mApi, CLIENT_ID, mImage); 
          download.execute(); 
         } 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 


        } 

       } 
      }); 

     } 

    }; 

    timer.schedule(doAsynchronousTask, 0,50000);//execute in every 50000 ms 

} 
@Override 
    protected Boolean doInBackground(Void... params) { 
try { 
     if (mCanceled) { 
      return false; 
     } 

     // Get the metadata for a directory 
     Entry dirent = mApi.metadata(mPath, 1000, null, true, null); 

     if (!dirent.isDir || dirent.contents == null) { 
      // It's not a directory, or there's nothing in it 
      mErrorMsg = "File or empty directory"; 
      return false; 
     } 

     // Make a list of everything in it that we can get a thumbnail for 
     ArrayList<Entry> thumbs = new ArrayList<Entry>(); 
     for (Entry ent: dirent.contents) { 
      if (ent.thumbExists) { 
       // Add it to the list of thumbs we can choose from 
       thumbs.add(ent); 
      } 
     } 

     if (mCanceled) { 
      return false; 
     } 

     if (thumbs.size() == 0) { 
      // No thumbs in that directory 
      mErrorMsg = "No pictures in that directory"; 
      return false; 
     } 

     // Now pick a random one 
     int index = (int)(Math.random() * thumbs.size()); 
     Entry ent = thumbs.get(index); 
     String path = ent.path; 
     mFileLen = ent.bytes; 


     String cachePath = mContext.getCacheDir().getAbsolutePath() + "/" + IMAGE_FILE_NAME; 
     try { 
      mFos = new FileOutputStream(cachePath); 
     } catch (FileNotFoundException e) { 
      mErrorMsg = "Couldn't create a local file to store the image"; 
      return false; 
     } 

     // This downloads a smaller, thumbnail version of the file. The 
     // API to download the actual file is roughly the same. 
     mApi.getThumbnail(path, mFos, ThumbSize.BESTFIT_960x640, 
       ThumbFormat.JPEG, null); 
     if (mCanceled) { 
      return false; 
     } 

     mDrawable = Drawable.createFromPath(cachePath); 
     // We must have a legitimate picture 
     return true; 

    } catch (DropboxUnlinkedException e) { 
     // The AuthSession wasn't properly authenticated or user unlinked. 
    } catch (DropboxPartialFileException e) { 
     // We canceled the operation 
     mErrorMsg = "Download canceled"; 
    } catch (DropboxServerException e) { 
     // Server-side exception. These are examples of what could happen, 
     // but we don't do anything special with them here. 
     if (e.error == DropboxServerException._304_NOT_MODIFIED) { 
      // won't happen since we don't pass in revision with metadata 
     } else if (e.error == DropboxServerException._401_UNAUTHORIZED) { 
      // Unauthorized, so we should unlink them. You may want to 
      // automatically log the user out in this case. 
     } else if (e.error == DropboxServerException._403_FORBIDDEN) { 
      // Not allowed to access this 
     } else if (e.error == DropboxServerException._404_NOT_FOUND) { 
      // path not found (or if it was the thumbnail, can't be 
      // thumbnailed) 
     } else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) { 
      // too many entries to return 
     } else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) { 
      // can't be thumbnailed 
     } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) { 
      // user is over quota 
     } else { 
      // Something else 
     } 
     // This gets the Dropbox error, translated into the user's language 
     mErrorMsg = e.body.userError; 
     if (mErrorMsg == null) { 
      mErrorMsg = e.body.error; 
     } 
    } catch (DropboxIOException e) { 
     // Happens all the time, probably want to retry automatically. 
     mErrorMsg = "Network error. Try again."; 
    } catch (DropboxParseException e) { 
     // Probably due to Dropbox server restarting, should retry 
     mErrorMsg = "Dropbox error. Try again."; 
    } catch (DropboxException e) { 
     // Unknown error 
     mErrorMsg = "Unknown error. Try again."; 
    } 
    return false; 
} 
protected void onProgressUpdate(Long... progress) { 
     int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5); 
     //mDialog.setProgress(percent); 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     //mDialog.dismiss(); 
     if (result) { 
      // Set the image now that we have it 
      mView.setImageDrawable(mDrawable); 
     } else { 
      // Couldn't download it, so show an error 
      showToast(mErrorMsg); 
     } 
    } 

我從我的活動onCreate調用這個我已經計劃了的AsyncTask。我的異步任務基本上從服務器下載一些數據。主要活動允許用戶登錄到服務器,然後我去提取數據。我面臨的問題是每個AsyncTask都創建自己的線程。而且,這些都不會完成。線程狀態始終在運行。有沒有辦法檢查新的AsyncTask是否只在早期完成時啓動。

+0

你不應該能夠從UI一個單獨的線程運行的AsyncTask讓我很驚訝,這不是崩潰。 – DeeV

+0

如果您打算實施週期性功能,則應考慮使用服務而不是活動。 –

+1

http://stackoverflow.com/questions/7494515/android-can-i-chain-async-task-sequentially-starting-one-after-the-previous-as和http://stackoverflow.com/questions/10048958/android-calling-asynctask-right-after-an-finished- – user1690588

回答

0

嘗試這種方式

public ArrayBlockingQueue<Runnable> threadList = new ArrayBlockingQueue<Runnable>(
     2000, true); 
public ThreadPoolExecutor threadPool = new ThreadPoolExecutor(0, 
     2000, 1000, TimeUnit.MILLISECONDS, threadList); 

TimerTask t=new TimerTask() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        //your code here 
       } 
      };  

ThreadPool.execute(t); 
0

timer.schedule(doAsynchronousTask, 0,50000);//execute in every 50000 ms負責重複性工作。

作爲每Timer類的日程安排的描述(TimerTaskObject,INT開始,INT repete)方法,這將重複一個特定的TimerTask定義時間間隔,建議你到如下改變它,

timer.schedule(doAsynchronousTask, 1000);//execute once after one second 
+0

但是這個任務只運行一次 – Ankuj

+0

所以你想運行多次?爲什麼? – Lucifer

+0

這是因爲我正在檢查服務器上的數據是否已更改。如果是,請下載該數據。 – Ankuj