如何在运行时确定是否通过amazon appstore或Google play安装了应用程序

时间:2013-01-19 11:37:13

标签: android packages apk package-managers

在运行时确定是否通过Amazon Android Appstore或Google Play安装了应用程序的建议最佳做法是什么?我想避免为我的应用程序可用的每个appstore生成单独的APK。

我首选的方法是使用PackageManger.getInstallerPackageName。

根据Android API文档,这正是我正在寻找的: 检索安装包的应用程序的包名称。这确定了包裹来自哪个市场。

不幸的是,通过Amazon Appstore(包括KF)安装的应用程序似乎返回null,这是用户手动安装APK时所期望的。

如果有其他方法可以告诉我。

1 个答案:

答案 0 :(得分:4)

除了返回null的问题之外,getInstallerPackageName()可能会被篡改并被用来返回不准确的信息。

我已经研究了很长时间了,我能想出的最好的解决方案是有一个包含这些信息的常量文件:

public class Constants {

    public static final int GOOGLE_PLAY = 0;
    public static final int AMAZON = 1;
    public static final int BUILT_FOR = GOOGLE_PLAY;
}

只需在此文件中使用AMAZON代替GOOGLE_PLAY,并为每个应用商店构建一次apk。

在代码的其他地方,您可以使用以下方法检查:

if(Constants.BUILT_FOR == Constants.GOOGLE_PLAY) {
    //Google Play build
}

if(Constants.BUILT_FOR == Constants.AMAZON) {
    //Amazon Build
}
相关问题