【发布时间】:2012-06-04 06:49:02
【问题描述】:
我需要检测我的应用程序是从 google play 或其他市场安装的,我如何获取此信息?
【问题讨论】:
-
Google 推出了新的 Sideload Prevention API:developer.android.com/guide/app-bundle/sideload-check?hl=en-419
标签: android google-play sideloading
我需要检测我的应用程序是从 google play 或其他市场安装的,我如何获取此信息?
【问题讨论】:
标签: android google-play sideloading
PackageManager 类提供了getInstallerPackageName 方法,该方法将告诉您安装您指定的包的包名。侧载应用不会包含值。
编辑:注意@mttmllns'answer below 关于亚马逊应用商店。
【讨论】:
仅供参考,亚马逊商店的最新版本PackageManager.getInstallerPackageName() 最终将"com.amazon.venezia" 设置为"com.amazon.venezia",并与Google Play 的"com.android.vending" 形成对比。
【讨论】:
我使用此代码检查构建是从商店下载还是侧载:
public static boolean isStoreVersion(Context context) {
boolean result = false;
try {
String installer = context.getPackageManager()
.getInstallerPackageName(context.getPackageName());
result = !TextUtils.isEmpty(installer);
} catch (Throwable e) {
}
return result;
}
科特林:
fun isStoreVersion(context: Context) =
try {
context.packageManager
.getInstallerPackageName(context.packageName)
.isNotEmpty()
} catch (e: Throwable) {
false
}
【讨论】:
isNotEmpty() 结尾
如果您正在考虑识别和限制侧载应用。谷歌已经提出了解决问题的解决方案。
您可以按照以下方式关注
项目的build.gradle:
buildscript {
dependencies {
classpath 'com.android.tools.build:bundletool:0.9.0'
}
}
App 模块的build.gradle:
implementation 'com.google.android.play:core:1.6.1'
扩展应用程序的类:
public void onCreate() {
if (MissingSplitsManagerFactory.create(this).disableAppIfMissingRequiredSplits()) {
// Skip app initialization.
return;
}
super.onCreate();
.....
}
通过这种集成,谷歌将自动识别是否缺少任何拆分 apk,并显示一个弹出窗口“安装失败”,它还会重定向到用户可以正确安装应用程序的 Play 商店下载屏幕通过 Google Play 商店。
查看此link 了解更多信息。
希望这会有所帮助。
【讨论】: