gpt4 book ai didi

java - 检查是否从 Google Play 安装了应用程序

转载 作者:行者123 更新时间:2023-11-30 10:19:55 28 4
gpt4 key购买 nike

我在开发应用程序时添加了 Google Play 规则明确禁止的功能。因此,我希望为那些从 Google Play 安装应用程序而未创建不同的 apk 的用户禁用此特定功能。这意味着我需要根据代码确定应用程序是从 Google Play 安装还是仅通过下载 apk 安装。

有什么办法可以实现吗?

最佳答案

这应该适用于该目的,取自 HockeyAppSDK for Android Github library :

protected static boolean installedFromMarket(WeakReference<? extends Context> weakContext) {
boolean result = false;

Context context = weakContext.get();
if (context != null) {
try {
String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
// if installer string is not null it might be installed by market
if (!TextUtils.isEmpty(installer)) {
result = true;

// on Android Nougat and up when installing an app through the package installer (which HockeyApp uses itself), the installer will be
// "com.google.android.packageinstaller" or "com.android.packageinstaller" which is also not to be considered as a market installation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT) || TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT2))) {
result = false;
}

// on some devices (Xiaomi) the installer identifier will be "adb", which is not to be considered as a market installation
if (TextUtils.equals(installer, INSTALLER_ADB)) {
result = false;
}
}

} catch (Throwable ignored) {
}
}

return result;
}

但是,我建议为此构建两个不同的应用程序。如果您在 Google Play 中加入了禁止代码,尽管此方法不会帮助执行它,但它会成为您的字节码的一部分,并且很可能会被 Google 禁止。通过创建另一种风格将其从您的代码中删除会更加清晰。

关于java - 检查是否从 Google Play 安装了应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369912/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com