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

两盒软妹~` 提交于 2020-12-27 08:37:02

问题


Is there any way to know if an application has been downloaded from Amazon App Store or Google Play Store? 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.


回答1:


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. I'm not sure what the Amazon Appstore is, but it should be easy to test using the above code.

Via ADB:

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

Example:

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

installerPackageName=com.android.vending



回答2:


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.amazon.venezia" is the Amazon Appstore, and
null when it was sideloaded

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:

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.



来源:https://stackoverflow.com/questions/11072079/how-to-know-if-an-app-has-been-downloaded-from-google-play-or-amazon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!