一、前言:
我們在app中,有的時候想跳轉(zhuǎn)到應(yīng)用市場,去更新apk或者去評分。我們就需要在自己本app中跳轉(zhuǎn)到應(yīng)用市場。
原理十分簡單,構(gòu)建一個ACTION_VIEW標(biāo)記的Intent,并給一個如下結(jié)構(gòu)的 Uri 即可:
//商店中使用包名來唯一標(biāo)識區(qū)分應(yīng)用
"market://details?id= "+ getPackageName()
在 Android 平臺上,正常情況下手機中的應(yīng)用商店應(yīng)該是 Google Play
但是由于各種你懂我也懂的原因,國內(nèi)基本上無法使用 Google Play 服務(wù)。
好在廣泛的第三方應(yīng)用市場大多都實現(xiàn)了這個接口。
二、使用:
1. 直接跳轉(zhuǎn)就行
//這里開始執(zhí)行一個應(yīng)用市場跳轉(zhuǎn)邏輯,默認(rèn)this為Context上下文對象
//跳轉(zhuǎn)到應(yīng)用市場,非Google Play市場一般情況也實現(xiàn)了這個接口
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
2. 異常情況
//存在手機里沒安裝應(yīng)用市場的情況,跳轉(zhuǎn)會包異常,做一個接收判斷
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + getPackageName()));
if (intent.resolveActivity(getPackageManager()) != null) {
//可以接收
startActivity(intent);
} else {
//沒有應(yīng)用市場,我們通過瀏覽器跳轉(zhuǎn)到Google Play
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
//這里存在一個極端情況就是有些用戶瀏覽器也沒有,再判斷一次
if (intent.resolveActivity(getPackageManager()) != null) {
//有瀏覽器
startActivity(intent);
}
}
需要注意的就是,如果界面跳轉(zhuǎn)失敗,會拋出異常,因此能否跳轉(zhuǎn)需要進(jìn)行判斷。
3. 總結(jié):
根據(jù)以上,同理使用以下Uri進(jìn)行替換:
//跳轉(zhuǎn)到商店搜索界面,并搜索開發(fā)者姓名
Uri.parse("market://search?q=pub:Author) Name");
//跳轉(zhuǎn)到商店搜索界面,并搜索關(guān)鍵詞
Uri.parse("market://search?q=Keyword)");
三、跳轉(zhuǎn)指定應(yīng)用市場:
1. 跳轉(zhuǎn)到應(yīng)用寶
//跳轉(zhuǎn)到指定的市場,例如騰訊應(yīng)用寶
public void goToTencentMarket() {
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.setClassName("com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity");
startActivity(goToMarket);
}
- setClass:跳轉(zhuǎn)到與該工程下的(同一個Application中的)activity或者service
- setClassName:跳轉(zhuǎn)到不同Applicaiton的activity或者service
2. 國內(nèi)主要Android應(yīng)用市場包名
- com.tencent.android.qqdownloader 騰訊應(yīng)用寶
- com.qihoo.appstore 360手機助手
- com.baidu.appsearch 百度手機助手
- com.xiaomi.market 小米應(yīng)用商店
- com.huawei.appmarket 華為應(yīng)用商店
- com.wandoujia.phoenix2 豌豆莢
- com.dragon.android.pandaspace 91手機助手
- com.hiapk.marketpho 安智應(yīng)用商店
- com.yingyonghui.market 應(yīng)用匯
- com.tencent.qqpimsecure QQ手機管家
- com.mappn.gfan 機鋒應(yīng)用市場
- com.pp.assistant PP手機助手
- com.oppo.market OPPO應(yīng)用商店
- cn.goapk.market GO市場
- zte.com.market 中興應(yīng)用商店
- com.yulong.android.coolmart 宇龍Coolpad應(yīng)用商店
- com.lenovo.leos.appstore 聯(lián)想應(yīng)用商店
- com.coolapk.market cool市場
參考作者:Leon_hy
鏈接:http://www.itdecent.cn/p/050dcda2603d
鏈接:https://blog.csdn.net/qq_35678553/article/details/77161036