有没有办法让我的 Android 应用检测用户手机上是否启用了侧面加载?用户已经在他们的手机上安装了我的应用程序。现在我想以编程方式测试用户是否启用了旁加载,即用户是否更改了设置以允许从场外来源安装应用程序。有没有API可以获取这些信息?是否有存储在某处的设置可供我以编程方式查询(无需用户交互)?
最佳答案
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
public static boolean isSideloadingEnabled(Context ctx) {
final ContentResolver cr = ctx.getContentResolver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Pull from Secure settings for (API < 17)
return Settings.Secure.getInt(cr, Settings.Secure.INSTALL_NON_MARKET_APPS, 0) == 1;
} else {
//Setting was moved to Global settings in (API >= 17)
return Settings.Global.getInt(cr, Settings.Global.INSTALL_NON_MARKET_APPS, 0) == 1;
}
}
INSTALL_NON_MARKET_APPS
的 Javadoc 状态:
Whether the package installer should allow installation of apps downloaded from sources other than Google Play. 1 = allow installing from other sources 0 = only allow installing from Google Play
关于Android API 用于检测是否启用侧加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21465764/