我們Android會打很多渠道包蒙挑,以區(qū)分各個市場的使用情況俯抖,但是App內(nèi)部升級的包一般是默認(rèn)官方渠道包颊糜,這樣,如果用戶直接應(yīng)用內(nèi)升級的話稻据,就會造成所有用戶的渠道都會變成官方渠道艾猜,所以,可能會需要用到跳轉(zhuǎn)到渠道對應(yīng)的市場的功能捻悯。目前匹配了以下幾個市場匆赃,如有需要更多的市場,可以自己獲取到對應(yīng)市場的包名后秋度,按規(guī)則添加即可:
// 跳轉(zhuǎn)到應(yīng)用寶的網(wǎng)頁版地址
private final static String WEB_YINGYONGBAO_MARKET_URL = "https://a.app.qq.com/o/simple.jsp?pkgname=com.dk.collage";
private final static String MARKET_PKG_NAME_MI = "com.xiaomi.market";
private final static String MARKET_PKG_NAME_360 = "com.qihoo.appstore";
private final static String MARKET_PKG_NAME_VIVO = "com.bbk.appstore";
private final static String MARKET_PKG_NAME_OPPO = "com.oppo.market";
private final static String MARKET_PKG_NAME_YINGYONGBAO = "com.tencent.android.qqdownloader";
private final static String MARKET_PKG_NAME_ANZHI = "cn.goapk.market";
private final static String MARKET_PKG_NAME_HUAWEI = "com.huawei.appmarket";
private final static String MARKET_PKG_NAME_BAIDU = "com.baidu.appsearch";
private final static String MARKET_PKG_NAME_LIQU = "com.liqucn.android";
private final static String MARKET_PKG_NAME_SOUGOU = "com.sougou.androidtool";
private final static String MARKET_PKG_NAME_MEIZU = "com.meizu.mstore";
/**
* 跳轉(zhuǎn)到渠道對應(yīng)的市場炸庞,如果沒有該市場,就跳轉(zhuǎn)到應(yīng)用寶(App或者網(wǎng)頁版)
* @param context
*/
public void goToAppMarket(Context context) {
try {
Uri uri = Uri.parse("market://details?id=" + context.getApplicationContext().getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 過去當(dāng)前渠道channelName
String channelName = CommonUtil.getCurrentChannelName();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, 0);
String pkgName = "";
switch (channelName) {
case "normal":
pkgName = MARKET_PKG_NAME_YINGYONGBAO;
break;
case "baidu":
pkgName = MARKET_PKG_NAME_BAIDU;
break;
case "huawei":
pkgName = MARKET_PKG_NAME_HUAWEI;
break;
case "oppo":
pkgName = MARKET_PKG_NAME_OPPO;
break;
case "qihu360":
pkgName = MARKET_PKG_NAME_360;
break;
case "vivo":
pkgName = MARKET_PKG_NAME_VIVO;
break;
case "xiaomi":
pkgName = MARKET_PKG_NAME_MI;
break;
case "yingyongbao":
pkgName = MARKET_PKG_NAME_YINGYONGBAO;
break;
case "anzi":
pkgName = MARKET_PKG_NAME_ANZHI;
break;
case "liqu":
pkgName = MARKET_PKG_NAME_LIQU;
break;
case "sougou":
pkgName = MARKET_PKG_NAME_SOUGOU;
break;
case "meizu":
pkgName = MARKET_PKG_NAME_MEIZU;
break;
default:
pkgName = MARKET_PKG_NAME_YINGYONGBAO;
break;
}
// 給一個默認(rèn)的 應(yīng)用寶
if (NullUtil.isNull(pkgName)) {
pkgName = MARKET_PKG_NAME_YINGYONGBAO;
}
// 篩選指定包名的市場intent
if (resInfo.size() > 0) {
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo resolveInfo = resInfo.get(i);
String packageName = resolveInfo.activityInfo.packageName;
if (packageName.toLowerCase().equals(pkgName)) {
Intent intentFilterItem = new Intent(Intent.ACTION_VIEW, uri);
intentFilterItem.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
context.startActivity(intentFilterItem);
return;
}
}
}
// 未匹配到荚斯,跳轉(zhuǎn)到應(yīng)用寶網(wǎng)頁版
goToYingYongBaoWeb(context);
} catch (Exception e) {
e.printStackTrace();
// 發(fā)生異常埠居,跳轉(zhuǎn)到應(yīng)用寶網(wǎng)頁版
goToYingYongBaoWeb(context);
}
}
/**
* 跳轉(zhuǎn)到應(yīng)用寶網(wǎng)頁版 多客拼團(tuán)頁面
*/
public void goToYingYongBaoWeb(Context context) {
try {
Uri uri = Uri.parse(WEB_YINGYONGBAO_MARKET_URL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}