hi i am doing an app which send user location to server for every 6 secs using fused locationapi inside service.the problem is that i don't know how to schedule it to make it run for every 6 secs.i am newbie to android.so anybody help me.Thank you in advance
-
You can use AlarmManager or JobScheduler - for more info refer: https://developer.android.com/topic/performance/scheduling.html look for their tutorial and implement using any one of the two. After implementing in case you face any issue then post it in here. – Shadow Droid Nov 11 '16 at 08:07
2 Answers
I suggest using rxJava library - it is very convenient:
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;
}

- 3,217
- 2
- 13
- 11
You can solve this by creating a foregroundService
. This might solve your problem of your service getting killed on low memory. A foreground service
is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. Source
For a good examples of a foreground Service read through this thread: Android - implementing startForeground for a service?
Also to be informed when the location changes check out this answer: https://stackoverflow.com/a/8600896/5183341
I also created a similar service than you are trying to do and allways had problems with my service getting killed on some devices. (Like the LG2 with API19). I tryed alot of different solotions like using an alarmmanager which restarts the service and so on. The only which worked solid on all devices was using a foregroundService.

- 1
- 1

- 3,958
- 5
- 45
- 70