回答
对于“如何与服务工作”,见
Services - Android
Services in Android - Vogella
这里是一个专注于使用AlarmManager“每10分钟”的明确的解决方案:https://stackoverflow.com/a/10222390/2591556
假设你有一个正在运行服务
用户AlarmManager每10分钟运行一次服务
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, YourService.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 600000, pi); // Millisec * Second * Minute
}
'RTC_WAKEUP'不适用于'服务'。您将不得不使用'WakefulBroadcastReceiver'来做到这一点 – NinjaCoder 2017-11-09 03:06:14
请解释一下,它是通过报警管理器启动待定意图并验证并正常工作。但是自从最初在Android上发布很多东西以来,它已经有2年了。 – 2017-11-09 05:07:43
你可以写一个后台服务: Running in a Background Service
,并启动服务每10-11分钟(的AlarmManager省电行为的原因),或者确切时间(需要shedule每次下一次执行)与AlarmManager.setExact
例子:
private static PendingIntent createClockIntent(Context context) {
Intent intent = new Intent(context.getString(R.string.widget_broadcast_clock_update));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
public static void startClockAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
clockIntent = createClockIntent(context);
alarmManager.setRepeating(AlarmManager.RTC, 0,
600000, clockIntent);
}
- 1. Azure - 移动服务 - 每10秒运行后台任务
- 2. 如何每10分钟在一个服务中运行一个函数?
- 3. 如何让Ruby每10分钟运行一次任务?
- 4. 每10分钟在后台执行一次数据库操作
- 5. Objective-C的iPhone编程:后台任务每10分钟
- 6. 在后台运行应用程序超过10分钟
- 7. android报警管理器每15分钟运行一次后台服务
- 8. 服务如何在后台运行 - Android
- 9. 服务在后台运行?
- 10. 在后台每分钟运行一次powershell脚本
- 11. 使用后台服务获取每1分钟或每1km更改的当前位置使用后台服务
- 12. 在android上运行闹钟的后台服务?
- 13. 运行后台服务ruby
- 14. 如何在后台启动nodejs并重启所有10分钟
- 15. 如何继续在后台运行前台服务也在android
- 16. 如何每分钟运行一次AsyncTask?
- 17. 如何设置cron每40分钟/ 25分钟运行脚本?
- 18. iOS 8在10分钟后停止在后台播放音频
- 19. 试图在后台运行服务
- 20. Android服务不会在后台运行
- 21. 永远在后台运行服务..? Android
- 22. C#/ Mono在后台运行服务器
- 23. 不断在后台运行服务
- 24. GPS服务在后台运行(Android)
- 25. 在后台运行Webrick服务器?
- 26. 在后台运行的android服务
- 27. iOS,ObjC:服务在后台运行
- 28. 从android远程服务每30分钟运行一次方法
- 29. 如何在HTML中每10分钟执行一次Javascript?
- 30. 如何在循环中使用c#每10分钟运行一次函数?
可能重复http://stackoverflow.com/questions/8230606/android -run-thread-in-service-every-x-seconds) – duggu 2015-01-21 08:19:04
请问为什么?我问的原因是因为Android很多次,你有多种方式来做同样的事情。 – 2015-01-21 08:35:07