如何在应用程序中设置刷新间隔

时间:2023-01-20 21:25:59

hi I am new in android. I have created one application and I use preferences for setting. I have used list preferences set to 1,10 ,60 minute. Now i want to apply a function so that another activity should be refreshed in each time interval.

嗨我是android新手。我创建了一个应用程序,并使用首选项进行设置。我使用列表首选项设置为1,10,6分钟。现在我想应用一个函数,以便在每个时间间隔刷新另一个活动。

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

                        } else if (idx == 1) {
                            // System.out.println("2");

                        } else if (idx == 2) {
                            System.out.println("3");
                        } else {
                            System.out.println("4");
                        }

                        return true;
                    }

This is code for preferences screen. Now i have another class named DetailPage. This class we have to reload. Can You please complete this code. I have tried much but not able to do. I am new in android and want to implement this. Please help me and complete this code. How i will load another class in certain time interval? I mean to say that if we select 1 minute then the activity should reload each 1 minute. If we select 10 minutes then application should reload each 10 minute and ... Help me to implement this

这是首选项屏幕的代码。现在我有另一个名为DetailPage的类。这个课我们要重装。你能否完成这段代码。我尝试了很多但却无法做到。我是android新手,想要实现这个。请帮助我并完成此代码。我将如何在特定时间间隔内加载另一个班级?我的意思是说,如果我们选择1分钟,那么活动应该每1分钟重新加载一次。如果我们选择10分钟,那么应用程序应该每10分钟重新加载一次......帮助我实现这一点

2 个解决方案

#1


0  

Maybe this will help you: Handler.postDelayed

也许这会对你有帮助:Handler.postDelayed

With Handlers you can post Runnables to be executed at some time in the future.

使用Handler,您可以在将来的某个时间发布Runnables。

There is also the possibility to register at android's alarm service, but that's not what you want, i guess.

还有可能在Android的报警服务注册,但我想这不是你想要的。

#2


0  

in an activity you can use the ScheduledFuture class from android to execute an runnable at a fixed interval with the scheduleAtFixedRate function see http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html for more information

在一个活动中,你可以使用android中的ScheduledFuture类以一个固定的间隔执行一个runnable,使用scheduleAtFixedRate函数,请参阅http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html获取更多信息

public class DetailPage extends Activity { 
    private final ScheduledExecutorService checkCurrentTime=Executors.newScheduledThreadPool(1);  
    private ScheduledFuture scheduledTimeChecker ;

    private final Runnable timeChecker = new Runnable() {       
        @Override
        public void run() {             
            doInBackground(str); 
        }
    };

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        startTimeSchecker();
    } 

    protected Void doInBackground(String Url) { 
        detailed_news = dh.get_detail_news(id[i],catagoryid); 
        allData.add(detailed_news); 
    } 

    private void startTimeSchecker(){        
        if(scheduledTimeChecker==null ||scheduledTimeChecker.isCancelled()){        
            scheduledTimeChecker =checkCurrentTime.scheduleAtFixedRate(timeChecker, 10, 60,java.util.concurrent.TimeUnit.SECONDS );
        }
    }
} 

#1


0  

Maybe this will help you: Handler.postDelayed

也许这会对你有帮助:Handler.postDelayed

With Handlers you can post Runnables to be executed at some time in the future.

使用Handler,您可以在将来的某个时间发布Runnables。

There is also the possibility to register at android's alarm service, but that's not what you want, i guess.

还有可能在Android的报警服务注册,但我想这不是你想要的。

#2


0  

in an activity you can use the ScheduledFuture class from android to execute an runnable at a fixed interval with the scheduleAtFixedRate function see http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html for more information

在一个活动中,你可以使用android中的ScheduledFuture类以一个固定的间隔执行一个runnable,使用scheduleAtFixedRate函数,请参阅http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html获取更多信息

public class DetailPage extends Activity { 
    private final ScheduledExecutorService checkCurrentTime=Executors.newScheduledThreadPool(1);  
    private ScheduledFuture scheduledTimeChecker ;

    private final Runnable timeChecker = new Runnable() {       
        @Override
        public void run() {             
            doInBackground(str); 
        }
    };

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        startTimeSchecker();
    } 

    protected Void doInBackground(String Url) { 
        detailed_news = dh.get_detail_news(id[i],catagoryid); 
        allData.add(detailed_news); 
    } 

    private void startTimeSchecker(){        
        if(scheduledTimeChecker==null ||scheduledTimeChecker.isCancelled()){        
            scheduledTimeChecker =checkCurrentTime.scheduleAtFixedRate(timeChecker, 10, 60,java.util.concurrent.TimeUnit.SECONDS );
        }
    }
}