Android:检查谷歌设置位置是否已启用

标签 android location google-play-services

我正在开发一款使用 Google Play 服务的应用。在某些手机上,客户端会为位置返回 null。发生这种情况是因为 locaiton 选项未在 google 设置上启用,如所附图片所示。

如何以编程方式检查 Android 应用程序中是否启用了谷歌设置位置?

http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/

最佳答案

可以检查 google play 服务本身是否可用。按照此处的说明进行操作: https://developer.android.com/training/location/retrieve-current.html

即使启用了 Google Play Services 定位服务,getLastLocation() 也有可能返回 null。例如,在重新启用它们之后。但是,您可以使用以下 Intent 将您的用户带到 Google Play 服务位置设置:

Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS");
startActivity(settings);

最后,还可以检查 Android 位置服务是否已启用:

public static boolean isLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    }else{
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }


} 

关于Android:检查谷歌设置位置是否已启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067256/

相关文章:

android - 一个 facebook 应用程序用于多个 android 应用程序

android - 使用 react-native 的 android 上的空白屏幕(仅限发布)

android - com.google.android.gms.common.api.ApiException : 17: API: Drive. API_CONNECTIONLESS 在此设备上不可用

java - RTSP 描述 header 但没有响应

android - onBackPressed() 方法在 android 中无法正常工作

android - Droid 3 上的 ListView 背景为灰色

android - Android 8 或 9 的后台地理围栏不起作用

java - Android 3.1及以上版本自动开启定位服务

c++ - PC IP地址到位置坐标

android - 如何取消应用程序与谷歌驱动器帐户的链接