我创建了一个在Boot完成后启动的后台服务。在后台服务中,我启动处理程序,间隔时间为15分钟,检查我的数据库并从数据库导出,如果在db中找到新内容,但我的处理程序在40之后启动此服务; 77; 60; 46; ......分钟!
以下是我如何启动处理程序的代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Start handler
mHandler = new Handler();
startRepeatingTask();
return START_STICKY;
}
void startRepeatingTask() {
mStatusChecker.run();
}
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try{
writeLogcat.main("SERVICE Started");
//Check internet connection
isInternetWorking();
//Check for datum
removeOldFiles();
}finally {
mHandler.postDelayed(mStatusChecker, 900000);
}}};
从log cat文件中我有这个处理程序启动间隔
23.01.2017 00:30:01 - SERVICE Started
23.01.2017 01:47:23 - SERVICE Started
23.01.2017 02:39:51 - SERVICE Started
23.01.2017 03:15:08 - SERVICE Started
23.01.2017 03:23:53 - SERVICE Started
23.01.2017 03:33:25 - SERVICE Started
我怎样才能做得更好?