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

问题描述:

我正在使用谷歌播放服务的应用程序。在某些电话上,客户端返回空位置。发生这种情况是因为locaiton选项未在谷歌设置中启用,如附图所示。Android:检查是否启用谷歌设置位置

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

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

+0

使用'LocationManager'检查不同的供应商 – tyczj

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示出了用户,设置以允许的当前位置信息源的配置。

+3

为什么你第一lm'的'值设置为'null',然后检查它是否等于如果是,则将其值设置为系统位置服务,如果只需在一行中声明它,如下所示:'LocationManager lm =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);' –

可以检查谷歌播放服务本身是否可用。按照说明在这里: https://developer.android.com/training/location/retrieve-current.html

即使启用Google Play服务位置服务,getLastLocation()也将返回null。例如,在重新启用它们之后。但您可以通过以下意图把你的用户在谷歌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); 
    } 


} 
+0

嗨!这个解决方案非常好。只有一件事,Settings.Secure.LOCATION_PROVIDERS_ALLOWED在API Level 17中已弃用,而Kitkat的API Level 19.您在if else块中缺少API Level 18。我对吗? –

+0

对不起,延迟回复。我不确定,但是我在2014年写了这篇文章,所以事情从那时起就发生了变化。 – gflarity

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

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

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

/** 
    * 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); 
     } 
    }