简体   繁体   English

如何知道应用是否已从Google Play或亚马逊下载?

[英]How to know if an app has been downloaded from Google Play or Amazon?

Is there any way to know if an application has been downloaded from Amazon App Store or Google Play Store? 有没有办法知道某个应用程序是从Amazon App Store还是从Google Play商店下载的? I meant within the app itself, of course. 当然,我的意思是在应用程序内部。

I have deployed an app to both sites and I rather like to know from where the customer has downloaded it within the application. 我已经为这两个站点部署了一个应用程序,而我更愿意知道客户在应用程序中的下载位置。 I know, I can deploy different applications to each service, but this adds some maintenance work that could be avoided if there were some manner to solve it just with a conditional within the app using the same package. 我知道,我可以为每个服务部署不同的应用程序,但这增加了一些维护工作,如果有一些方法可以使用相同的包在应用程序中使用条件来解决它。

In Code: 在代码中:

final PackageManager packageManager = getPackageManager();

try {
    final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
    if ("com.android.vending".equals(packageManager.getInstallerPackageName(applicationInfo.packageName))) {
        // App was installed by Play Store
    }
} catch (final NameNotFoundException e) {
    e.printStackTrace();
}

"com.android.vending" tells you it came from the Google Play Store. “com.android.vending”告诉你它来自Google Play商店。 I'm not sure what the Amazon Appstore is, but it should be easy to test using the above code. 我不确定亚马逊Appstore是什么,但使用上面的代码测试应该很容易。

Via ADB: 通过亚行:

adb shell pm dump "PACKAGE_NAME" | grep "vending"

Example: 例:

adb shell pm dump "com.android.chrome" | grep "vending"

installerPackageName=com.android.vending

While in most cases you can get the store name by including a check similar to this: 虽然在大多数情况下,您可以通过包含类似于此的检查来获取商店名称:

final PackageManager packageManager = getPackageManager();

try {
    final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
    if ("com.android.vending".equals(packageManager.getInstallerPackageName(applicationInfo.packageName))) {
        // App was installed by Play Store
    }  else if ("com.amazon.venezia".equals(packageManager.getInstallerPackageName(applicationInfo.packageName))) {
        // App was installed by Amazon Appstore
    } else {
        // App was installed from somewhere else
    }
} catch (final NameNotFoundException e) {
    e.printStackTrace();
}

"com.android.vending" is Google Play Store and “com.android.vending”是Google Play商店和
"com.amazon.venezia" is the Amazon Appstore, and “com.amazon.venezia”是亚马逊Appstore,和
null when it was sideloaded 当它被侧载时为null

The results could be unreliable however, as for example during beta testing a store might not set this value, and besides it's possible to sideload your app specifying the installer's package name that could be interpreted as a store name: 然而,结果可能是不可靠的,例如在beta测试期间,商店可能不会设置此值,此外,可以侧载您的应用程序,指定可以解释为商店名称的安装程序包名称:

adb install -i <INSTALLER_PACKAGE_NAME> <PATH_TO_YOUR_APK>

You might want to consider having different application IDs for different stores, for example "com.example.yourapp" for Google and "com.example.yourapp.amazon" for Amazon -- you can easily set those in your Gradle script. 您可能需要考虑为不同的商店设置不同的应用程序ID,例如Google的“com.example.yourapp”和亚马逊的“com.example.yourapp.amazon” - 您可以在Gradle脚本中轻松设置这些ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM