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

  • What's the difference between DataOutputStream and ObjectOutputStream?
  • How to find last activity from which current activity is opened in android?
  • Android L Elevation Effect in Pre L (only using elevation property)
  • How can we turn automatic brightness off in android?
  • Start ICS emulator without Menu button
  • Why is registering IabBroadcastReceiver in Activity a bad idea?
  • Using Toolbar with Fragments
  • Invalidate cache in Picasso
  • error opening trace file: No such file or directory (2)
  • android:syncable=true for a ContentProvider?
  • Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent
  • Android when ViewPager insert as Child get error android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5035)
  • 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)
        {
            switch(event)
            {
            case GPS_EVENT_STARTED:
                // do your tasks
                break;
            case GPS_EVENT_STOPPED:
                // do your tasks
                break;
            }
        }
    });
    

    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.

    Implementation:

    /**
     * Following broadcast receiver is to listen the Location button toggle state in Android.
     */
    private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
        @Override
        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

    Reference: https://developer.android.com/reference/android/location/LocationManager.html#MODE_CHANGED_ACTION

    You can get location mode with

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

    Value returned should be one of this:

    android.provider.Settings.Secure.LOCATION_MODE_BATTERY_SAVING
    android.provider.Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
    android.provider.Settings.Secure.LOCATION_MODE_OFF
    android.provider.Settings.Secure.LOCATION_MODE_SENSORS_ONLY
    

    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.