Android API 用于检测是否启用侧加载

标签 android sideloading

有没有办法让我的 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/

相关文章:

android - 在 C4droid 中包含新的头文件

Android,侧载应用程序并通过 Google Play 保持最新状态

swift - 离线 MapBox 侧载合并

android - 当从图像选择器打开相机时,应用程序 "Lost connection to device."

android - 如何将 DialogFragment 上的按钮设置为平面样式?

iOS 应用程序不断崩溃

android - 如何知道应用程序是从 google play 还是侧载安装的?

cordova - UWP导入第三方代码签名证书失败

android - 在 Android 上生成和导出 RSA key 对

javascript - 移动webkit内存消耗