如何使用线程在每小时,分钟应用刷新间隔

时间:2012-09-03 11:31:06

标签: java android multithreading

beerPref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // TODO Auto-generated method stub

             final ListPreference listrefresh = (ListPreference) preference;
             final int idx = listrefresh.findIndexOfValue((String) newValue);



             if(idx==0){
                // set refresh application in each 1 minute according to current time

             }
             else if(idx==1)
             {
                //  set refresh application in each 10 minute according to current time

             }
             else if (idx==2)
             {
                //  set refresh application in each 1 hour  according to current time
             }
             else
             {
                 //  set refresh application in each 24 hour according to current time
             }




            return true;
        }
    });

我必须在设置中使用这个plz告诉我如何应用线程或其他一些东西,以便我们的应用程序在每1分钟,10分钟刷新...同样在列表中设置prefnces plz post me code for这一点。

1 个答案:

答案 0 :(得分:2)

我更喜欢使用ScheduledExecutorService,然后您可以在方法参数中定义刷新间隔,如下所示。

    ExecutorService scheduler =  Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(RunableTask, initialDelay, delay , TimeUnit.MINUTES);

    //where delay is refresh interval and you can set initialDelay as 0;

以分钟为单位传递TimeUnit,然后采用可变延迟,该延迟从设置中获取值。