Listening to the turning on/off of the GPS can be done via LocationManager for the status (enabled/disabled) of the GPS/Network provider. This is different from the HERE Android SDK's GpsSignalListener which is for GPS signal lost (e.g when in a tunnel) and restore (i.e when you exit the tunnel) events.
So for listening to the on/off of the GPS of your phone/Android emulator, you need to subscribe to the broadcast messages from the Android system and check for LocationManager.PROVIDERS_CHANGED_ACTION
. See sample code snippet in an Activity:
@Override
protected void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
this.registerReceiver(gpsOnOffReceiver, iFilter);
}
private BroadcastReceiver gpsOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gpsOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkOn = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsOn || networkOn) {
// GPS is ON
Log.d("GPS", "GPS turned ON");
} else {
// GPS is OFF
Log.d("GPS", "GPS turned OFF");
}
}
}
};