我怎样才能把一个计时器在AsyncTask的,而无需创建另一个线程? [英] How can I put a timer in an AsyncTask without creating another thread?

查看:231
本文介绍了我怎样才能把一个计时器在AsyncTask的,而无需创建另一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我用来连接到一个网站(Twitter)上的异步任务。在某些情况下(例如,当Twitter发布更新失败)我想再次开始前等待10秒。正如我在一个异步任务,今年已经我不想启动另一个线程定时器。

任何建议将是巨大的:)。
code低于...

 类SendTwitAsyncTask扩展的AsyncTask<弦乐,太虚,太虚> {    @覆盖
    保护无效doInBackground(字符串... PARAMS){        串tokenTwit =参数[0];
        串tokenSecretTwit =参数[1];
        字符串strMessageBody =参数[2];        的accessToken aToken =新的accessToken(tokenTwit,tokenSecretTwit);        //初始化Twitter4J
        叽叽喳喳叽叽喳喳=新TwitterFactory()的getInstance()。
        twitter.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
        twitter.setOAuthAccessToken(aToken);
        aToken = NULL;        //创建一个鸣叫
        日期D =新的日期(System.currentTimeMillis的());
        字符串鸣叫= strMessageBody;        尝试{        状态= twitter.updateStatus(鸣叫);
        //为用户反馈..
        showNotification(推特,TWIT_SUCCESS);    }赶上(TwitterException TE){        showNotification(推特,TWIT_FAIL);
        te.printStackTrace();                       //去做
                       //发布鸣叫失败。再次尝试在10秒
    }        返回null;
    }} //结束类SendTwitAsyncTask


解决方案

有很多方法可以做到这一点。我可能会用Handler.postDelayed去你的情况。

创建Handler对象标记决赛在相同的范围作为您的AsyncTask

 最后的处理程序处理程序=新的处理程序();

然后调用postDelayed从里面的AsyncTask计划下一次运行,在必要时:

  handler.postDelayed(新的Runnable(){        公共无效的run(){
            新SmartTwitAsyncTask.execute(参数);
        }
    },10000);

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

解决方案

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

这篇关于我怎样才能把一个计时器在AsyncTask的,而无需创建另一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆