我在我的 Android 应用程序中编写了以下代码,以确定它是从哪个 Play 商店下载的:
public enum AppStore {
GOOGLE("com.android.vending", "market://details?id="),
AMAZON("com.amazon.venezia", "amzn://apps/android?p=");
private String packageName;
private String marketUrl;
private AppStore(String pName, String url) {
this.packageName = pName;
this.marketUrl = url;
}
public String getPackageName() {
return this.packageName;
}
public String getMarketUrl() {
return this.marketUrl;
}
public static AppStore getAppStore(String packageName) {
if(GOOGLE.packageName.equalsIgnoreCase(packageName)) {
return GOOGLE;
} else if(AMAZON.packageName.equalsIgnoreCase(packageName)) {
return AMAZON;
}
return null;
}
};
public static AppStore getAppStore(Context ctx) {
String packageName = ctx.getPackageName();
PackageManager pacman = ctx.getPackageManager();
String installer = pacman.getInstallerPackageName(packageName);
return AppStore.getAppStore(installer);
}
但我无法测试它。因为我总是从本地构建中获取空值。有什么方法可以测试它是否有效。