我用AlarmManager
每1分钟启动一次服务来执行一些后台工作。每1分钟后台工作一次,服务,我应该叫`stopSelf()`?
这里是代码片断:
Intent intent = new Intent(context, PostLocationService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, POST_LOCATION_REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + TEN_SECONDS_INTERVAL,
ONE_MINUTE_INTERVAL, pendingIntent);
PostLocationService
延伸Service
。在onStartCommand()
我做了一些工作,并在完成这项工作后致电stopSelf()
。
但是我知道如果这个服务还在运行,下次接收意向onStartCommand()
会再次执行并且需要完成工作。
这里的最佳实践是什么:每1分钟启动一项新服务或保持一项服务在运行并等待意图?
我已经添加了我的答案,请让我知道你是否想要更多的信息。 –