2016-11-11 37 views
0

您好我正在做一个应用程序发送用户位置服务器每6秒使用融合locationapi内service.the问题是,我不知道如何安排它使它运行每6秒钟。我是新手android.so任何人都可以帮助我。提前感谢您如何获取位置更新每6秒使用服务在Android中

+0

您可以使用AlarmManager或的jobscheduler - 更多信息请参考:https://developer.android.com/topic/performance/scheduling.html寻找他们的教程并使用其中任何一个来实施。如果遇到任何问题,请执行后将其发布到此处。 –

回答

0

您可以通过创建foregroundService解决此问题。这可能会解决您的服务在低内存中死亡的问题。前台服务

是用户主动意识到并没有对系统的候选 杀低时内存的服务。 Source

对于前台服务的一个很好的例子,通过这个线程读取:Android - implementing startForeground for a service?

也被告知当位置改变看看这个答案:https://stackoverflow.com/a/8600896/5183341


我还创建了一个类似的服务比你试图做的和总是有问题,我的服务在一些设备上遇难。 (如同API19的LG2)。我尝试了很多不同的方法,比如使用重新启动服务的警报管理器等等。唯一在所有设备上稳定运行的是使用foregroundService。

1

我建议使用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; 
} 
相关问题