我正在处理应用程序,并添加了Google Play规则明确禁止的功能。因此,我希望对于那些从Google Play安装应用程序而未创建其他APK的用户禁用此特定功能。这意味着我需要从代码中确定是从Google Play安装该应用程序,还是仅通过下载apk来确定。
有没有办法做到这一点?
这应该可以从HockeyAppSDK for Android Github 库获得:
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禁止。通过创建另一种样式,将其从代码中删除更加干净。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句