开发者

How can I put a timer in an AsyncTask without creating another thread?

开发者 https://www.devze.com 2023-03-25 01:12 出处:网络
I have an asynchronous task that I use to connect to a website (Twitter).In certain situations (eg. when Twitter post开发者_如何学Python updatefails) I want to wait 10 seconds before trying again.As I

I have an asynchronous task that I use to connect to a website (Twitter). In certain situations (eg. when Twitter post开发者_如何学Python 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


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


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.


 @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
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消