java - 如何使用线程在每小时、分钟内应用刷新间隔

标签 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;
        }
    });

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

最佳答案

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

关于java - 如何使用线程在每小时、分钟内应用刷新间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246986/

相关文章:

java - 我可以用 Java 编写 MS-SQL 过程吗?

java - 创建 while 和 for 循环来查看是否大于、等于或小于 B。然后打印结果

具有多线程的 C 矩阵

c++ - 在C++中创建线程时出错

Java - 信号量中的优先级

java - 我如何判断一个 joda-time DateTime 对象是否在另一个对象的 4 小时内?

java - 代码不工作(事件驱动编程)

android - 在 Activity 中嵌入日期选择器

c# - 在 vs2010 中开发 iphone\android 应用程序需要做什么?

java - 如何为除一两个以外的所有 API 请求添加拦截器?