简体   繁体   中英

Android location listener in Application background

I want to Implement a location listener in my app which will run in the background, even when the app is closed. I found a lot of result for implementing the location listener but I'm still confused.

My use-case is to get the location in all possible ways, whether it'd be GPS, Network provider or any other way. The app needs to update location periodically in the background

I found an example similar to below to get the precise location:

    LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

Few more examples at the link below:

https://zgrepcode.com/examples/android/android/location/locationmanager-implementations

this doesn't work very accurately, the listener didn't work in the background service.

So is this the best way to get the location in the background? Most of the posts I found seem old, do we have a better way to implement background location listener in newer API's?

I searched StackOverflow for the same but didn't find any relevant post, if there's a post which answers the same question, please share the link. Or let me know whats the best way to implement it.

Thanks in advance :-)

Apps can receive location updates only a few times each hour . Google is very protective regarding this information. Also active GPS, drains a lot of battery, so AGPS, is maybe better option if this is done manually.

Location updates are provided to background apps only a few times each hour.

An app is considered to be in the foreground if any of the following is true :

  • It has a visible activity, whether the activity is started or paused.
  • It has a foreground service.
  • Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. For example, if a foreground app binds to any of the following components within another app, that other app is considered to be in the foreground, input method editor (IME), Wallpaper service, Notification listener,Voice or text service.

If none of those conditions is true, the app is considered to be in the background.

Consider using FusedLocationProviderClient , but that has a dependency on Google Play Services . This is the best option. You can read more about it here

I made an app by using Jobscheduler. It working. But it has some conflict when the other app likenavigation sw is using location manager. I am looking forthe solution now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM