【问题标题】:Check if 'Access to my location' is enabled - Android检查是否启用了“访问我的位置” - Android
【发布时间】:2013-08-11 14:48:54
【问题描述】:

我有一个使用位置信息的安卓应用。但我注意到,如果用户在“设置”>“位置访问”中禁用“对我的位置的访问”,则不再有效。如何检查它是否已启用? 如果它被禁用,如何从我的应用程序中打开这些设置?

谢谢

已解决:

String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders == null || locationProviders.equals("")) {
    ...
    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    你可以这样检查:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Return a boolean
    

    编辑:

    如果要查看网络提供商:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Return a boolean
    

    编辑 2:

    如果要打开设置,可以使用这个intent:

    Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    

    【讨论】:

    • 您也可以检查网络提供商的可用性(将LocationManager.GPS_PROVIDER 替换为LocationManager.NETWORK_PROVIDER)。
    • 如果您想要 Wifi,请使用 NETWORK 提供商。查看文档和官方 Android 文章,例如:android-developers.blogspot.com/2011/06/…(请注意,现在也有更新的东西,通过 Google Play 服务,例如融合提供程序:developer.android.com/google/play-services/location.html
    • Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);效果更好,因为它提供了所有可用的提供程序
    • 谢谢 Charlie Collings,我会看看 fused 提供者
    • 我从未使用过Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);,但我会尝试一下。另外,我放了一个打开位置源设置的线索。
    【解决方案2】:

    也许这会有用 查看它讨论的有关位置服务的网站

    http://www.scotthelme.co.uk/android-location-services/

    【讨论】:

    • 好吧,好像是 Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);谢谢,知道如何从我的应用程序中打开它吗?
    【解决方案3】:

    不使用Settings.Secure 且不扫描所有位置提供程序的另一种方法是:

    LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
    int providersCount = locationManager.getProviders(true).size(); // Listing enabled providers only
    if (providersCount == 0) {
        // No location providers at all, location is off
    } 
    

    【讨论】:

      【解决方案4】:

      不幸的是,自 API 19 起,Settings.Secure.LOCATION_PROVIDERS_ALLOWED 的使用似乎已被弃用。

      一种新的方法是:

      int locationMode = Settings.Secure.getInt(
          getContentResolver(),
          Settings.Secure.LOCATION_MODE,
          Settings.Secure.LOCATION_MODE_OFF // Default value if not found
      );
      
      if (locationMode == Settings.Secure.LOCATION_MODE_OFF) {
          // Location is off
      }
      

      【讨论】:

        【解决方案5】:

        有两种方法可以查看位置, 1-使用GPS 2-使用网络提供商 最好检查这两个服务是否启用。为此使用此方法:)

        public boolean checkServices(){
            //check location service
            LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
                    locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                    return true;
            }
        }
        

        【讨论】:

        • 我不知道为什么有人给我-1这个...这个解决方案对我有用,我正在使用它。如果解决方案不适合你尝试另一个解决方案不要标记为-1白痴,
        猜你喜欢
        • 2014-07-26
        • 2015-12-02
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        相关资源
        最近更新 更多