1

I have an asynchronous task that I use to connect to a website (Twitter). In certain situations (eg. when Twitter post update fails) I want to wait 10 seconds before trying again. As I am already in an async task I dont want to start another thread for the timer.

Any suggestions would be great :). Code below...

        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
Mel
  • 6,214
  • 10
  • 54
  • 71
  • Another thread really would be the best way to do this, however if you use a [TimerTask][1], this thread is very simple. [1]: http://Another%20thread%20really%20would%20be%20the%20best%20way%20to%20do%20this.%20You%20can%20use%20a%20%5BTimerTask%5D%5B1%5D. – Phil Aug 03 '11 at 03:45
  • i have tried to add something more to your code.. try if it works/.. – ngesh Aug 03 '11 at 03:52

3 Answers3

8

There are many ways to do this. I'd probably go with Handler.postDelayed in your case.

Create a Handler object marked final at the same scope as your AsyncTask

final Handler handler = new Handler();

Then call postDelayed from inside the AsyncTask to schedule the next run, where necessary:

    handler.postDelayed(new Runnable() {

        public void run() {
            new SmartTwitAsyncTask.execute(param);
        }
    }, 10000);
Rich
  • 36,270
  • 31
  • 115
  • 154
4

I know it's old thread but for the sake of future readers like me I dont think you should put Thread.sleep(1000*10); in onPostExecute because this will block UI thread and all point of activetask is to keep UIthread free of work.

You can put it here:

protected String doInBackground(String... params) {

where it belongs.

Danilo C.
  • 194
  • 2
  • 13
  • This is not a good idea since it would fill up thread pool with sleeping threads and blocks running of newly created threads until some of the sleeping threads finish executing. – zxcmehran Apr 16 '18 at 17:17
0
 @Override
    protected String 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 status;

    }

        return status;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    if(status.equals("failed"))
    {
    Thrad.sleep(1000*10);
    new SmartTwitAsyncTask.execute(param);

    }
    if(status.equals("success"))
    {
    //proceed
    }

}
ngesh
  • 13,398
  • 4
  • 44
  • 60