웹 사이트 (트위터)에 연결하는 데 사용하는 비동기 작업이 있습니다. 특정 상황 (예 : Twitter 게시물 업데이트가 실패한 경우)에서 다시 시도하기 전에 10 초 동안 기다리고 싶습니다. 내가 비동기 작업에 이미 있기 때문에 타이머에 대한 다른 스레드를 시작하고 싶지 않습니다.다른 스레드를 만들지 않고 타이머를 AsyncTask에 넣을 수 있습니까?
모든 의견은 훌륭합니다. :) 아래 코드 ...
class SendTwitAsyncTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String tokenTwit = params[0];
String tokenSecretTwit = params[1];
String strMessageBody = params[2];
AccessToken aToken = new AccessToken(tokenTwit, tokenSecretTwit);
// initialize Twitter4J
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(aToken);
aToken=null;
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = strMessageBody;
try {
status = twitter.updateStatus(tweet);
// feedback for the user..
showNotification("Twitter", TWIT_SUCCESS);
} catch (TwitterException te) {
showNotification("Twitter ", TWIT_FAIL);
te.printStackTrace();
//TO DO
//POSTING TWEET HAS FAILED. TRY AGAIN IN 10 SECONDS
}
return null;
}
} //end class SendTwitAsyncTask
[TimerTask] [1]을 사용하면 다른 스레드가 실제로이 작업을 수행하는 가장 좋은 방법입니다.이 스레드는 매우 간단합니다. [1] : http : //Another%20thread%20really%20would%20be%20the%20best%20way%20to%20do%20this.%20You%20can%20use%20a%20%5BTimerTask%5D%5B1% 5D. – Phil
나는 코드에 뭔가 더 추가하려고 시도했다. – ngesh