Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

architecture of gps based application with runtime permission

Tags:

android

gps

From first sight things to get GPS coordinates looks simple(pseudocode):

private void onStart() {
   res = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
   if (res != PackageManager.PERMISSION_GRANTED) {
     requestPermissions();
   } else {
     startGps();
   }
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  if (all_ok)
    startGps();
}
private void startGps() {
  LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  LocationListener locationListener = new LocationListener() {...};
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

But how to handle async events:

  1. What if after locationManager.requestLocationUpdates user pause and resume application,

    1.1 Shall I call locationManager.requestLocationUpdates to recieve GPS data again?

    1.2 What if user disable GPS when my application is paused, after user resume my app do I get onProviderDisabled event?

  2. How handle runtime revoking of ACCESS_FINE_LOCATION permission if it is happened after locationManager.requestLocationUpdates call? What happening after revoking, does android report me this via onStatusChanged or onProviderDisabled?

  3. I hear that new android allow several windows on screen, what happens if one window would belong to my app, another to "system preferences", and user revoke ACCESS_FINE_LOCATION without pause/resume of my app?

like image 542
user1244932 Avatar asked Aug 06 '16 16:08

user1244932


1 Answers

  1. Answer to question 1

1.1: Its better to stop listening to GPS when your app is paused or stopped (onPause() and onStop()). This will save the battery life. So start listener again in onResume() method.

removeUpdates(locationListener)

If you dont want to removeUpdates, then you need not call call locationManager.requestLocationUpdates again.

1.2: Whenever app comes to foreground, do isProviderEnabled(provider) check and show a dialog to user.

To answer your question:

https://developer.android.com/reference/android/location/LocationManager.html

In case the provider is disabled by the user, updates will stop, and a provider availability update will be sent. As soon as the provider is enabled again, location updates will immediately resume and a provider availability update sent. Providers can also send status updates, at any time, with extra's specific to the provider. If a callback was supplied then status and availability updates are via onProviderDisabled(String), onProviderEnabled(String) or onStatusChanged(String, int, Bundle).

  1. Your application might crash if permission is revoked. Because you will not have access to location feature.

java.lang.SecurityException: Provider network requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission

  1. In Multi window devices, one app which is focused will be in resumed state, other app will be in paused state. And regarding the permission revoking I have answered in point 2.

If location is turned off from settings 3rd party apps will not be notified about it. You need to use isProviderEnabled(provider) method to check the location status.

like image 157
arungiri_10 Avatar answered Oct 13 '22 00:10

arungiri_10