Android:检查是否启用了Google设置位置

时间:2014-06-05 18:13:05

标签: android location google-play-services

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

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

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

4 个答案:

答案 0 :(得分:9)

可以检查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);
    }


} 

答案 1 :(得分:4)

 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显示用户,允许配置当前位置来源的设置。

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:0)

应该有一个更改,您可以在其中检查TextUtils.isEmpty(locationProviders)。它永远不会是空的。如果没有提供商(禁用位置设置),则其值为"null"。是的我的意思是价值"null",而不是null

相关问题