How can I monitor the status of the GPS adapter using the Android SDK?

I need my app monitor to hang or disconnect the GPS adapter. I donโ€™t care about the actual GPS functions working in coin or not - I need the status of a GPS adapter.

I can do it manually: String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED)

If "gps" is displayed, then it is active, otherwise not. But I canโ€™t find the documentation on receiving a notification when the GPS is on or off by the user. I tried to use addGpsStatusListener in the LocationManager, but this only notifies when the actual GPS fix occurs (for example, if the Maps application is used).

Any ideas?

+2
source share
1 answer

LocationContentObserver Nevermind, ;

getContentResolver().registerContentObserver(
    Settings.Secure.getUriFor(Settings.System.LOCATION_PROVIDERS_ALLOWED), 
            false, 
            new LocationContentObserver ( new Handler())
    );

class LocationContentObserver extends ContentObserver {
  public LocationContentObserver( Handler h ) {
    super( h );
  }

  public void onChange(boolean selfChange) {
      SendNotification(false);
  }
}
+6

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


All Articles