【问题标题】:Android: Checking if google settings location is enabledAndroid:检查是否启用了谷歌设置位置
【发布时间】:2014-07-26 20:43:33
【问题描述】:

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

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

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

【问题讨论】:

  • 使用LocationManager检查不同的供应商

标签: android location google-play-services


【解决方案1】:

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

即使启用了 Google Play 服务定位服务,getLastLocation() 也可能返回 null。例如,在重新启用它们之后。但是,您可以使用以下意图将您的用户带到 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);
    }


} 

【讨论】:

  • 嗨!这个解决方案真的很好。只有一件事,Settings.Secure.LOCATION_PROVIDERS_ALLOWED 在 API 级别 17 中已被弃用,而 Kitkat 的 API 级别为 19。您的 if else 块中缺少 API 级别 18。我说的对吗?
  • 抱歉回复延迟。不确定,但我在 2014 年写了这篇文章,所以从那以后情况肯定发生了变化。
【解决方案2】:
 LocationManager lm = null;
 boolean gps_enabled,network_enabled;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    try{
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch(Exception ex){}
    try{
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch(Exception ex){}

   if(!gps_enabled && !network_enabled){
        dialog = new AlertDialog.Builder(context);
        dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
        dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                context.startActivity(myIntent);
                //get gps
            }
        });
        dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();

    }

ACTION_LOCATION_SOURCE_SETTINGS 向用户显示允许配置当前位置源的设置。

【讨论】:

  • 为什么先将lm的值设置为null,然后检查是否等于null,如果是,则将其值设置为系统定位服务,当你可以在一行上声明它时,像这样:LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
【解决方案3】:

我认为这是最好的解决方案。

您可以通过以下代码检查:

    /**
     * This is used to check whether location is enabled or not.
     * @param mContext
     * @return
     */
    public static boolean isDeviceLocationEnabled(Context mContext) {
        int locMode = 0;
        String locProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            try {
                locMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locMode != Settings.Secure.LOCATION_MODE_OFF;
        }else{
            locProviders = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locProviders);
        }
    }

【讨论】:

    【解决方案4】:

    应该有一个变化,你检查TextUtils.isEmpty(locationProviders)。它永远不是空的。如果没有提供者(位置设置被禁用),则其值为"null"。是的,我的意思是价值"null",而不是null

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多