Android - 获取INSTALL_NON_MARKET_APPS布尔值

时间:2013-07-21 21:12:49

标签: android

我正在尝试绕过具有两个不同版本的INSTALL_NON_MARKET_APPS选项。 17级之前的API级别和17+级别。我运行它时得到一个空指针异常:

boolean unknownSource = false;
if (Build.VERSION.SDK_INT < 17) {
    unknownSource = Settings.Secure.getInt(null, Settings.Secure.INSTALL_NON_MARKET_APPS, 0) == 1;
} else {
    unknownSource = Settings.Global.getInt(null, Settings.Global.INSTALL_NON_MARKET_APPS, 0) == 1;
}

1 个答案:

答案 0 :(得分:3)

我认为API级别3是Settings.System变量已弃用并更改为Settings.Secure,然后在API级别17中,它已更改为Settings.Global

另外,我认为方法调用需要一个Context Content Resolver。

boolean unknownSource = false;
if (Build.VERSION.SDK_INT < 3) {
    unknownSource = Settings.System.getInt(getContentResolver(), Settings.System.INSTALL_NON_MARKET_APPS, 0) == 1;
}
else if (Build.VERSION.SDK_INT < 17) {
    unknownSource = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0) == 1;
} else {
    unknownSource = Settings.Global.getInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS, 0) == 1;
}