Android 如何知道应用程序是从 google play 还是 side-load 安装的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10809438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know an application is installed from google play or side-load?
提问by ttom
I need to detect my application is installed from google play or other market, how could I get this information?
我需要检测我的应用程序是从 google play 或其他市场安装的,我怎么能得到这个信息?
采纳答案by MattC
The PackageManager
class supplies the getInstallerPackageNamemethod that will tell you the package name of whatever installed the package you specify. Side-loaded apps will not contain a value.
本PackageManager
类提供的getInstallerPackageName方法,将告诉你的任何安装指定的软件包的名称。旁加载的应用程序将不包含值。
EDIT: Note @mttmllns' answer belowregarding the Amazon app store.
回答by mttmllns
And FYI apparentlythe latest version of the Amazon store finally sets PackageManager.getInstallerPackageName()
to "com.amazon.venezia"
as well to contrast with Google Play's "com.android.vending"
.
和FYI显然亚马逊商店的最新版本终于将PackageManager.getInstallerPackageName()
到"com.amazon.venezia"
,以及与谷歌Play的对比"com.android.vending"
。
回答by Philipp E.
I use this code to check, if a build was downloaded from a store or sideloaded:
我使用此代码来检查构建是从商店下载还是旁加载:
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;
}
Kotlin:
科特林:
fun isStoreVersion(context: Context) =
try {
context.packageManager
.getInstallerPackageName(context.packageName)
.isNotEmpty()
} catch (e: Throwable) {
false
}
回答by Naveen T P
If you are looking at identifying & restricting the side-loaded app. Google has come up with the solution to identify the issue.
如果您正在寻找识别和限制侧载应用程序。谷歌已经提出了确定问题的解决方案。
You can follow as below
你可以按照以下
Project's build.gradle
:
项目build.gradle
:
buildscript {
dependencies {
classpath 'com.android.tools.build:bundletool:0.9.0'
}
}
App module's build.gradle
:
应用模块的build.gradle
:
implementation 'com.google.android.play:core:1.6.1'
Class that extends Application:
扩展应用程序的类:
public void onCreate() {
if (MissingSplitsManagerFactory.create(this).disableAppIfMissingRequiredSplits()) {
// Skip app initialization.
return;
}
super.onCreate();
.....
}
}
With this integration, google will automatically identifies if there are any missing split apks and shows a popup saying "Installation failed" and it also redirects to Play store download screen where user can properly install the app via the Google Play store.
通过这种集成,谷歌将自动识别是否有任何丢失的拆分 apk 并显示一个弹出窗口说“安装失败”,它还会重定向到 Play 商店下载屏幕,用户可以在那里通过 Google Play 商店正确安装应用程序。
Check this linkfor more info.
查看此链接以获取更多信息。
Hope this helps.
希望这可以帮助。