How to Trigger Broadcast Receiver When Gps Is Turn On/Off

How to trigger broadcast receiver when gps is turn on/off?

This is useful when user want to trigger any action on turn On/Off location provides

You should add this action in manifest

<action android:name="android.location.PROVIDERS_CHANGED" />

and after add this action you can trigger your broadcast receiver

<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

And in your BroadcastReceiver class you have to implement LocationListener in that class which is given following below..

public class GpsLocationReceiver extends BroadcastReceiver {        
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
}

Android: detect when GPS is turned on/off (or when no app is using it anymore)

On API level 9 and higher you can use LocationManager.PROVIDERS_CHANGED_ACTION:

Broadcast intent action when the configured location providers change. For use with isProviderEnabled(String). If you're interacting with the LOCATION_MODE API, use MODE_CHANGED_ACTION instead.

Constant Value: "android.location.PROVIDERS_CHANGED"

Source

How with broadcast receiver should I subscribe to listen for location services change

This is useful when user want to trigger any action on turn On/Off location provides

You should add this action in manifest

<action android:name="android.location.PROVIDERS_CHANGED" />

and after add this action you can trigger your broadcast receiver

<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

And in your BroadcastReceiver class you have to implement LocationListener in that class which is given following below..

  public class GpsLocationReceiver extends BroadcastReceiver {        
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
//NO GPS
}else{
//Gps is on
}
}
}
}

IOS (Swift 3) How to trigger broadcast receiver when gps is turn on/off?

If user updates location services permission in settings and then enters the app (that was still loaded in memory), your CLLocationManagerDelegate implementation should receive authorization status update in function:


func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)

If you want to make action after first authorization positive change, just persist previou state somehow into user defaults or other persisting options



Related Topics



Leave a reply



Submit