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

我必须在设置中使用它,请告诉我我将如何应用线程或其他东西,以便我们的应用程序在每 1、1、10 分钟内刷新一次……根据列表首选项中的设置,请向我发布代码。

4

1 回答 1

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,然后采用可变延迟,该延迟从设置中获取值。

于 2012-09-03T11:34:48.537 回答