Suchi
Suchi

Reputation: 10039

Android - Is there a way to listen if GPS was enabled or disabled

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?

Upvotes: 3

Views: 6939

Answers (2)

Peter Knego
Peter Knego

Reputation: 80340

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).

Upvotes: 7

Awais Tariq
Awais Tariq

Reputation: 7754

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();
}

Upvotes: 5

Related Questions