Android Fused Location API - how to use PRIORITY_BALANCED_POWER_ACCURACY and PRIORITY_HIGH_ACCURACY in one application?

Android Fused Location API - how to use PRIORITY_BALANCED_POWER_ACCURACY and PRIORITY_HIGH_ACCURACY in one application?

Hi, I am developing an application that should send periodic location updates. I used the Fuse Location API with the expected intention. (update interval 60 seconds and a minimum size of 100 m). I registered a pending intent locationclient using PRIORITY_BALANCED_POWER_ACCURACY and the intent service. IntentService was called when the localization changed, and this worked fine for several test drives. But it only works sometimes. When it does not work, I want to use PRIORITY_HIGH_ACCURACY

I noticed that when I use PRIORITY_HIGH_ACCURACY , it gives good places, but the battery usage is quite large. So I decided to use both PRIORITY_HIGH_ACCURACY and PRIORITY_BALANCED_POWER_ACCURACY along with ActivityRecognition.

Initially, the program starts with PRIORITY_BALANCED_POWER_ACCURACY using LocationPendingIntent, and when there is no update for 2 minutes, I use the LocationListener (and onLocationChanged) method that implements PRIORITY_BALANCED_POWER_ACCURACY , and if there is still no location, I call PRIORITY_HIGH_ACCURACY .

The problem I am facing is that after switching to PRIORITY_HIGH_ACCURACY , the LocationPendingIntent (registered in PRIORITY_BALANCED_POWER_ACCURACY ) always uses PRIORITY_HIGH_ACCURACY after that. I remove location updates at the end of the onLocationChanged method by calling .removeupdates (locationListener); however, the GPS icon continues to be displayed every minute.

I thought about using static methods for the onLocationChanged method, but I understand that this is not a good practice for an Android application. Also, are there any known issues while using PendingIntent and onLocationChanged? Can you suggest a solution to my problem?

Permissions are set to:

 uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Any help in this regard would be greatly appreciated.

Thanks.

+4
source share
1 answer

(I understand that this is a very late answer, but, having just done this, perhaps the information helps someone). I wanted to do something very similar, and I had two problems:

  • I found that removeLocationUpdates failed when I used exactly the same parameters with which I initially requested updates, that is, I did not receive any errors, and the result callback suggested that the cancellation went fine. But after calling removeLocationUpdates, the location update simply continued. I found using parameters 0 and null, as in the code, here worked for me to stop updates successfully:

    Intent selfIntent = new Intent(ACTION_CHECK_LOCATION, null, getApplicationContext(), <ServiceClass>.class); PendingIntent selfPendingIntent = PendingIntent.getService(getApplicationContext(), 0, selfIntent, 0);

  • Perhaps I had a conflict between requestLocationUpdates and removeLocationUpdates calls. I did not want to accidentally stop the updates that I just requested, so I ended up with two separate classes, one of which concerned each type of update.

In my case, I did not have a oriented application; instead, I used services to update the background. Here you can see my code - https://github.com/Esri/arcgis-runtime-demos-android/tree/master/2015-DS/LocalGeofence - note that I am working with another SDK (Esri ArcGIS Runtime for Android SDK, I work for Esri), but you can ignore this bit and just look at the two services GeofenceServiceNormal and GeofenceServiceFast and how they interact with the Fused Location API.

I ended up with two classes of service (to avoid the conflict of requestLocationUpdates and removeLocationUpdates). Each service recognizes when it should change to another service (see HandleActionChangeToFastUpdates and handleActionChangeToNormalUpdates); then it calls removeLocationUpdates for itself and calls requestLocationUpdates for another class of service. This service worked well for me - maybe you can use a similar structure.

0
source

Source: https://habr.com/ru/post/1501509/


All Articles