Launch gps application architecture

At first glance, things to get GPS coordinates look simple (pseudo-code):

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:

  • What to do if after locationManager.requestLocationUpdates user pauses and resumes the application,

    1.1 Can I call locationManager.requestLocationUpdates to get GPS data again?

    1.2 What should I do if the user disables GPS when my application is paused, after the user has resumed my application, I receive the onProviderDisabled event?

  • How does a command descriptor revoke ACCESS_FINE_LOCATION permission if this happens after calling locationManager.requestLocationUpdates ? What happens after the cancellation, does the android notify me via onStatusChanged or onProviderDisabled ?

  • I heard that the new android allows several windows on the screen, what happens if one window belongs to my application, the other - "system preferences", and the user cancels ACCESS_FINE_LOCATION without pausing / resuming my application?

+6
source share
2 answers
  • Answer to question 1

1.1: It’s better to stop listening to GPS when your application is paused or stopped (onPause () and onStop ()). This will save battery life. So, run the receiver again in the onResume () method.

removeUpdates (locationListener)

If you do not want to removeUpdates, you do not need to call location locationManager.requestLocationUpdates again.

1.2. When the application comes to the forefront, check isProviderEnabled (the provider) and show the dialog to the user.

To answer your question:

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

If the provider is disabled by the user, updates will cease and a supplier availability update will be sent. As soon as the provider is turned on again, location updates will resume immediately and a supplier availability update will be sent. Suppliers can also send status updates at any time with additional data for the provider. If a callback was received, then state and availability updates are available via onProviderDisabled (String), onProviderEnabled (String) or onStatusChanged (String, int, Bundle).

  1. Your application may crash if permission is revoked. Because you will not have access to the location function.

java.lang.SecurityException: ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission is required for the provider network

  1. In multi-window devices, one application that is focused will be able to resume, another application will be paused. And regarding recall recall, I answered in paragraph 2.

If the location is disconnected from the settings, third-party applications will not be notified about this. To check the status of the location you need to use the isProviderEnabled method (provider).

+1
source

please follow this link to fully describe how to add runtime permission in android, as well as a demonstration of permission to run in real time.

https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

-one
source

All Articles