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

前端 未结 4 1477
太阳男子
太阳男子 2021-02-13 02:24

I\'m on Android 2.3.7 and I\'m trying to write a BroadcastReceiver that will get called when the user turns GPS on/off (ie: by tapping on the \"use GPS satellites\" in the optio

4条回答
  •  鱼传尺愫
    2021-02-13 02:38

    Starting from API 19 you can listen for the intent action LocationManager.MODE_CHANGED_ACTION.

    You can also get the current location mode as follows:

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

    The value returned should be one of these:

    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
    

提交回复
热议问题