【问题标题】:Android detect location settingsAndroid 检测位置设置
【发布时间】:2013-09-30 03:07:04
【问题描述】:

我正在我的应用中使用新的 Location API,我想知道如何检测我的用户是否在手机上启用了位置访问权限。

我正在设置 LocationClient 并运行 LocationRequest。在执行此操作之前,我将如何运行检查以查看是否可以实际获取用户位置?

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    在您的 onConnected 中,您可以调用如下方法:

    private boolean checkLocationProviders()
    {
        if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        {
            return true;
        }
        else
        {
            if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                return true;
            else
                return false;
        }
    }
    

    如果返回false,可以这样打开设置界面:

    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    

    【讨论】:

    • 嗨,fasteque,当 startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); ,当它返回活动时,有没有办法知道它已经返回,并再次检查GPS?例如我们可以为此目的使用 startActivityForResult 吗?
    • @Elye 我猜你可以使用startActivityForResult 这样你就知道用户何时离开了设置屏幕,试一试看看是否真的如此。在这里您可以找到一些提示:stackoverflow.com/questions/5987379/… - 否则您可以查看您的活动的onResume。另请注意,新的 Google Play 服务版本 (7.0) 将改进此机制,如下所述:android-developers.blogspot.ch/2015/03/…
    【解决方案2】:

    这对你有帮助

        // flag for GPS status
    boolean isGPSEnabled = false;
    
    // flag for network status
    boolean isNetworkEnabled = false;
    
    // flag for GPS status
    boolean canGetLocation = false;
    
    Location location = null; // location
    double latitude; // latitude
    double longitude; // longitude
    
         // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    
    // Declaring a Location Manager
    protected LocationManager locationManager;
    
     public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);
    
            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多