Android Fused Location API - how to use PRIORITY_BALANCED_POWER_ACCURACY
and PRIORITY_HIGH_ACCURACY
in one application?
Hi,
I am developing an applicaiton which need to send periodic location updates.
I have used Fused Location API with a locaiton pending intent. (update interval 60 seconds and minimumDisplacement 100m). I have registered a locationclient pending intent with PRIORITY_BALANCED_POWER_ACCURACY
and intent service. IntentService was getting called when locaiton changed and this has worked fine for few test drives. But this only works sometimes. When it does not work, I wish to use the PRIORITY_HIGH_ACCURACY
I have noticed that when I use PRIORITY_HIGH_ACCURACY
, it gives good locations but the battery usage is quite high. So I have decided to use both PRIORITY_HIGH_ACCURACY
and PRIORITY_BALANCED_POWER_ACCURACY
along with ActivityRecognition.
Initially program starts with PRIORITY_BALANCED_POWER_ACCURACY
using LocationPendingIntent and when there is no update for 2 mins, I use LocationListener (and onLocationChanged method) implementing PRIORITY_BALANCED_POWER_ACCURACY
and if there is still no location I call PRIORITY_HIGH_ACCURACY
.
The problem I face here is, once this switches to PRIORITY_HIGH_ACCURACY
, LocationPendingIntent (which is registered with PRIORITY_BALANCED_POWER_ACCURACY
) is always using PRIORITY_HIGH_ACCURACY
after this. I am removing the location updates at the end of the onLocationChanged method by calling .removeupdates(locationListener); however the GPS icon keeps showing up every minute.
I thought of using static methods for the onLocationChanged method but I understand that this is not good practice for an android app. Also is there any known issues with using PendingIntent and onLocationChanged at the same time? Can you suggest a solution to my problem?
The permissions are set to:
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Any help regarding this would be highly appreciated.
Thanks.
(I realise this is a very late response, but having just tackled this, maybe the information benefits someone). I wanted to do something very similar and I had two issues:
I found removeLocationUpdates failed when I used exactly the same parameters with which I initially requested the updates - that is, I did not get any errors, and the result callback suggested the cancelation had worked OK. But after calling removeLocationUpdates, the location updates just kept happening. I found using parameters 0 and null as in the code here worked for me to stop the updates successfully:
Intent selfIntent = new Intent(ACTION_CHECK_LOCATION, null, getApplicationContext(), <ServiceClass>.class); PendingIntent selfPendingIntent = PendingIntent.getService(getApplicationContext(), 0, selfIntent, 0);
I may have had a conflict between the calls to requestLocationUpdates and removeLocationUpdates. I didn't want to accidentally stop the updates I'd just requested, so I ended up with two separate classes, one to deal with each type of update.
In my case I did not have a foregrounded app, instead I was using services for background updates. You can see my code here - https://github.com/Esri/arcgis-runtime-demos-android/tree/master/2015-DS/LocalGeofence - note that I'm working with another SDK (Esri ArcGIS Runtime for Android SDK, I work for Esri) but you can ignore that bit and just look at two services GeofenceServiceNormal and GeofenceServiceFast, and how they're interacting with the Fused Location API.
I ended up with two service classes (to avoid the requestLocationUpdates and removeLocationUpdates conflict). Each service recognizes when it must change to the other service (see handleActionChangeToFastUpdates and handleActionChangeToNormalUpdates); then it calls removeLocationUpdates for itself, and calls requestLocationUpdates for the other service class. This service worked well for me - maybe you can make use of a similar structure.