嗨,我正在做一个应用程序,它发送用户的位置到服务器每6秒使用融合位置app内service.the的问题是,我不知道如何安排它,使其运行每6秒。我是新手,以android.so任何人帮助me.Thank你提前
发布于 2016-11-11 16:45:23
我建议使用rxJava库-它非常方便:
Subscription repeatingTask =
Observable.interval(6, Timeunit.SECONDS)
// receive notification on background thead
.observeOn(Schedulers.computation())
.map( t -> {
// do your work;
return result;
}
.map(result -> {
sendResult();
return result
})
// notify UI
.observeOn(AndroidSchedulers.mainThread)
.subscribe(r -> {
// update UI
});
// unsubscribe when polling is no longer needed:
if (subscription != null && !subscription.isUnsubscribed()){
subscription.unsubscribe();
subscription = null;
}发布于 2016-11-11 15:57:39
您可以通过创建foregroundService来解决此问题。这可能会解决您的服务在内存不足时被终止的问题。前台服务
是用户主动感知的服务,并且不是系统在内存不足时要终止的候选服务。Source
有关前台服务的一个很好的示例,请通过此线程进行阅读:Android - implementing startForeground for a service?
另外,要在位置更改时获得通知,请查看此答案:https://stackoverflow.com/a/8600896/5183341
我还创建了一个与您正在尝试的服务类似的服务,并且我的服务在某些设备上总是出现问题。(就像带有API19的LG2 )。我尝试了很多不同的解决方案,比如使用一个重新启动服务的alarmmanager等等。唯一在所有设备上都能正常工作的就是使用foregroundService。
https://stackoverflow.com/questions/40543318
复制相似问题