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 theLOCATION_MODE API
, useMODE_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
How to Debug Android Application Line by Line Using Eclipse
Rotating Image on a Canvas in Android
Problem to Load Flv Video in Webview
Android Setting with Textview for Hebrew Text
Error:Execution Failed For Task ':App:Transformclasseswithjarmergingfordebug'
Checking Internet Connection on Android
Flutter Run Error:You Have Not Accepted the License Agreements
Google Firebase Sign Out and Forget User in Android App
Using Jaxb with Google Android
How to Scale Bitmap to Screen Size
How to Create Custom Baseadapter for Autocompletetextview
How to Fix Android.Os.Deadobjectexception Android X
Android M Camera Intent + Permission Bug
Android - Controlling a Task with Timer and Timertask