3

I write this code to receive location updates -

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 5000, intent, 0);
manager.requestLocationUpdates(selectProvider(), 0, 0, launchIntent);

I select the gps provider if its available, otherwise the network provider. Is there a way I could switch back to the gps one if it gets enabled later?

Does the system broadcast an intent when gps provider is enabled or disabled? Could I set up a listener for the same?

Suchi
  • 9,741
  • 22
  • 65
  • 107
  • Not an android dev, but I know it's possible - the wifi monitor app I use (wifi manager by kostya vasilyev) can detect when wifi's enabled and updates its widget in sync with the wifi on/off status. Various GPS apps also can detect gps being enabled/disabled as well. – Marc B Sep 22 '11 at 21:08
  • possible duplicate of [How do I find out if the GPS of an Android device is enabled](http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled) – Matt Ball Sep 22 '11 at 21:10
  • 1
    not duplicate - other question looks for a one time check (and activate which is no longer possible), this question wants to listen for a status change – Richard Le Mesurier Feb 18 '14 at 15:27

2 Answers2

7

You can detect if GPS status changed, e.g. if a GPS satellite fix was acquired.

Look at GpsStatus.Listener. Register it with locationManager.addGpsStatusListener(gpsStatusListener).

Peter Knego
  • 78,855
  • 10
  • 118
  • 147
  • 1
    This one is deprecated now and [GnssStatus](https://developer.android.com/reference/android/location/GnssStatus.Callback) should be used instead – Wahib Ul Haq May 28 '18 at 02:32
  • @WahibUlHaq Can you provide example how to use `GnssStatus`? because I havent find any best practice about how to listen to gps status enable/disable – Yarin Shitrit Jan 10 '21 at 14:16
5
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_LONG).show();

}

public void onProviderDisabled(String provider) {
    Toast.makeText(this, "GPS disconnected", Toast.LENGTH_LONG);
        Toast.makeText(getApplicationContext(), "Connected",
                Toast.LENGTH_LONG);


    Toast.makeText(this, "Please Switch on GPS." + provider,
            Toast.LENGTH_LONG).show();
}
Awais Tariq
  • 7,423
  • 5
  • 27
  • 51
  • 5
    Unclear anser. What is this code? Is it implementation of some interface (what interface)? How do one should use it (e.g where should one pass it)? – Luten Dec 05 '16 at 07:32
  • I agree with @luten, it is not clear what are you answering – Dullahan Jan 26 '17 at 15:16
  • Guys, this was answered back in 2011, there must be many new and efficient ways handle such kind of situations. – Awais Tariq Jan 28 '17 at 19:38
  • For those are still clueless about the interface, it is referring to [LocationListener](https://developer.android.com/reference/android/location/LocationListener) – Wahib Ul Haq May 27 '18 at 23:29