【问题标题】:GPS is not enabled but isProviderEnabled() is returning trueGPS 未启用,但 isProviderEnabled() 返回 true
【发布时间】:2012-04-12 04:57:50
【问题描述】:

我可以使用 isProviderEnabled() 检查 GPS 是否开启。如果它没有打开,我正在启动意图,以便用户可以启用 GPS。 最后,我再次检查用户是否启用了 GPS。 如果用户没有启用 GPS 并出来,isProviderEnabled() 仍然返回 NULL。 可能是什么问题 ?请指导我。

    String provider = LocationManager.GPS_PROVIDER;
    // Check if GPS is enabled
    boolean enabled = myLocationManager.isProviderEnabled(provider);

    if (!enabled) {
                    // GPS not enabled
        Log.d("", "Provider " + provider + " is not enabled");
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
                    // Consider the case when user does not enable GPS and come out. 
    } else {
        Log.d("", "Provider is enabled");

    }       

            // Re-check if user has enabled or not. (Note: case: user has not enabled GPS)
    enabled = myLocationManager.isProviderEnabled(provider);
    if(!enabled)
    {

        Log.d("","provider not enabled");
    }
            else
            {
                    // Control is coming here though user has not enabled GPS in settings
                    Log.d("","GPS is enabled");
            }

谢谢, 双实验室

【问题讨论】:

    标签: android


    【解决方案1】:

    使用此代码检查 GPS 启用并告诉我发生了什么,

    private void CheckEnableGPS(){
        String provider = Settings.Secure.getString(getContentResolver(),
          Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
           if(!provider.equals("")){
               //GPS Enabled
            Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
              Toast.LENGTH_LONG).show();
           }else{
            Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
               startActivity(intent);
           }   
       }
    

    【讨论】:

    • 谢谢,完美如我所愿。
    • 对我不起作用,我在启用和禁用 gps 的情况下尝试了它,两次都得到了相同的吐司。
    • @BillGary 这可能是因为您的网络或被动提供商仍然处于活动状态。该提供程序字符串应包含所有启用的提供程序,而不仅仅是 GPS。他对 null 的检查只告诉我们启用了 A 提供程序,而不是实际启用了。
    • 我有点害怕在 Api 19 中弃用 Settings.Secure.LOCATION_PROVIDERS_ALLOWED 但这确实有效。
    • 从 KitKat (API 19) 开始,您需要使用 Settings.Secure.LOCATION_MODE 并将其与 0 进行比较(它是 int 而不是字符串)。
    【解决方案2】:

    我在实际的物理设备上遇到过这个问题。

    我在我的物理 Android 设备上使用模拟位置进行了一些测试,然后使用 GPS 切换回真实位置(代码中没有所有模拟位置)。 此时,无论 GPS 是否停止,应用程序都将始终返回“true”(GPS 已激活),并且由于某种原因不再注册真实位置。

    在这种情况下,重启物理设备即可解决问题。

    【讨论】:

    • 我开始担心我的应用程序。这帮助很大。谢谢!另外,有没有办法以编程方式重置模拟位置?我想确保我的应用程序的完整性,即使对于我的那些稍微臭名昭著的受众群体也是如此。
    • 我不确定你在问什么,但对我来说(当我尝试过时)这是我可以重置位置的唯一方法。
    • 只需打开和关闭位置就可以完成这项工作,这就是我正在使用我的应用程序所做的事情,只需让用户重新启动他的 GPS,然后使用BroadcastReciever 来确保用户实际重新启动它。
    【解决方案3】:

    遇到同样的问题,isProviderEnabled() 在设备上返回 true

    根据 user370305 的回答,我将方法更新如下:

    private boolean isLocationEnabled() {
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    
            String provider = Settings.Secure.getString(getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    
            return !provider.equals("");
    
        } else {
    
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            return gps || network;
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2016-06-22
      相关资源
      最近更新 更多