【问题标题】:Two checks of Content Resolver for Allow Mock Locations providing different results内容解析器的两次检查允许模拟位置提供不同的结果
【发布时间】:2012-01-02 08:51:22
【问题描述】:

当“应用程序>设置>开发>允许模拟位置”未选中时,我有以下代码在模拟器(OS 2.3.3)中评估为真。我希望该方法返回 false,但它返回 true。

public static boolean isMockLocationSet(Context context) {
    if (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 0) {
        return false; 
    }
    else {
        return true;                    
    }
}

以下更改按预期返回 false(顺便说一句,.equals 或 .ContentEquals 哪个更好?):

public static boolean isMockLocationSet(Context context) {
    if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0")) { //.contentEquals("0")
        return false; 
    }
    else {
        return true;                    
    }
}

我更喜欢第一个示例,因为它应该允许可能存在空值的情况,分配默认值 0 并且仍然允许执行逻辑而不会失败(实际上,我怀疑这种情况可能存在但尚未证明- 例如,制造商在没有建立所有这些设置的情况下实现了 Android(例如,允许模拟位置之类的设置会以 null 开始)...等到用户检查设置后再将 1(或未选中时为 0)写入表) .

那么问题是什么?好吧,我从错误报告中感觉到不同的设备以不同的方式处理此检查,但无法访问所有设备类型,我正在寻找有关如何处理一般/最佳的建议。另外,为什么第一个例子不起作用?

【问题讨论】:

    标签: android geolocation android-contentresolver


    【解决方案1】:

    好吧,我决定简单地使用以下检查:

    public static boolean isMockLocationSet(Context context) { 
        if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).contentEquals("1")) { 
            return true;  
        } 
        else { 
            return false;                     
        } 
    } 
    

    当然,这种方法可以适当地处理 null 的情况——返回 false。

    我不能说为什么原始问题中的代码不起作用...可能是 SDK 中的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多