How to detect when user turn on/off gps state?

I want to block the user from changing wifi,gps and loading settings from my application.The user need not on/off wifi and gps while running my application.(From notification bar).Is there any BroadcastReceiver exist for listening gps on/off?

Thanks in Advance

  • ConnectivityManager getActiveNetworkInfo null pointer exception
  • Android title bar removal
  • How to create n project with ADT r20 without including the android support library?
  • Android AdMob memory usage
  • ActionBar 'up' button destroys parent activity, 'back' does not
  • Lollipop elevation on concave outline
  • Related posts:

    html phonegap android : numeric soft keyboard has next instead of go button
    WiFi-Direct communication between Mac/Windows and Android
    Get Recent and Running application list not processes
    Spring Rest Template usage causes EOFException
    play store alpha test download link not working
    How to add “Next” on android keyboard
  • In Cordova, how can I specify different package names for ios and android?
  • Facing issues in resolving the 9-patch images in android studio
  • Exoplayer: How to play audio through ear piece?
  • How can I test in-app-billing with a non-published app?
  • Android Catch Notes App Like Circle Menu
  • Ideal Android Studio gitignore file
  • 7 Solutions collect form web for “How to detect when user turn on/off gps state?”

    You can listen the GPS status with a GpsStatus.Listener
    and register it with the LocationManager.

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
        public void onGpsStatusChanged(int event)
            case GPS_EVENT_STARTED:
                // do your tasks
            case GPS_EVENT_STOPPED:
                // do your tasks

    You need to have access to the context (for example in an “Activity” or “Application” class).

    Well I did a lot of Digging and addGpsStatusListener(gpsStatusListener) is deprecated in API 24. Well for me this didn’t even work. Here is another alternative to this.

    If you want in your app, to listen to the GPS state change (I mean On/Off by user). Broadcast is the best approach.


     * Following broadcast receiver is to listen the Location button toggle state in Android.
    private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                // Make an action or refresh an already managed state.

    Don’t forget to register and unregister this efficiently in Fragment/Activity Lifecycle as well.

    registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

    For example if you are using in a Fragment, register in onResume and unregister in onDestroy if, you are taking user to the settings to enable the Location Switch. Unregistering in the onStop will not function since, your activity goes to onPause and Fragment is stopped.

    Well there may be many answers for this solution but this one is easy to manage and use

    This is not possible. You can’t control/restrict the state of the hardware on your whim. This would be dangerous to have in the APIs and rightfully so no such API exists.

    You can register a BroadcastReceiver for listening to Intent Action PROVIDERS_CHANGED_ACTION. This will broadcast when the configured Location provider changes. You can refer this link.

    You can detect status of GPS by following way.

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

    Also check this SO link for better understanding.

    Starting from API 19 you can register a BroadcastReceiver for listening to intent action LocationManager.MODE_CHANGED_ACTION


    You can get location mode with

        int locationMode = android.provider.Settings.Secure.getInt(context.getContentResolver(), android.provider.Settings.Secure.LOCATION_MODE);
    } catch (android.provider.Settings.SettingNotFoundException e)

    Value returned should be one of this:


    You cannot use GpsStatus.Listener to do this. You must use broadcast receiver. Use LocationManager.PROVIDERS_CHANGE_ACTION. Not use Intent.ACTION_PROVIDER_CHANE.
    Good luck !

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.