我需要在后台运行的每1分钟启动一次服务,即使我的应用程序没有运行
这是代码的一部分:`
我使用计时器每1分钟处理一次呼叫
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask hourlyTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
try {
Intent intent = new Intent(ServiceMain.this, LocationMonitoringService.class);
startService(intent);
// Toast.makeText(testServiceMain.this, "test", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(hourlyTask, 3, 1000 * 10);
}
Run Code Online (Sandbox Code Playgroud)
"谢谢你