Android: Check if Location Services Enabled using Fused Location Provider

前端 未结 5 659
囚心锁ツ
囚心锁ツ 2021-01-31 10:59

According to the Android documentation:

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework t

5条回答
  •  时光说笑
    2021-01-31 11:31

    As now in 2020

    Latest , Best and shortest way is

        public boolean isLocationEnabled()
        {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    // This is new method provided in API 28
                        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                        return lm.isLocationEnabled();
                    } else {
    // This is Deprecated in API 28
                        int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
                                Settings.Secure.LOCATION_MODE_OFF);
                        return  (mode != Settings.Secure.LOCATION_MODE_OFF);
    
                    }
        }
    

提交回复
热议问题