解决思路
- 根据厂商名,获取指定应用市场包名,和网页版地址
- 如果本地已安装应用市场软件,则跳转至应用市场下载
- 如果本地未安装应用市场软件,则跳转至网页版应用市场下载
- 如果未提供网页版应用市场平台,则跳转至该应用市场的下载地址
- 以上条件均不满足,提示无法找到对应的应用市场
实现代码
kotlin
object MarketUtils {
const val MARKET_CHANNEL_HUAWEI = "HUAWEI"
const val MARKET_CHANNEL_HONOR = "HONOR"
const val MARKET_CHANNEL_XIAOMI = "XIAOMI"
const val MARKET_CHANNEL_VIVO = "VIVO"
const val MARKET_CHANNEL_OPPO = "OPPO"
// not supported already
const val MARKET_CHANNEL_TENCENT = "TENCENT"
const val MARKET_CHANNEL_ALIBABA = "ALIBABA"
const val MARKET_CHANNEL_BAIDU = "BAIDU"
const val MARKET_CHANNEL_GOOGLE = "GOOGLE"
const val MARKET_CHANNEL_SAMSUNG = "SAMSUNG"
const val MARKET_CHANNEL_LENOVO = "LENOVO"
const val MARKET_PACKAGE_HUAWEI = "com.huawei.appmarket"
const val MARKET_PACKAGE_HONOR = "com.hihonor.appmarket"
const val MARKET_PACKAGE_XIAOMI = "com.xiaomi.market"
const val MARKET_PACKAGE_VIVO = "com.bbk.appstore"
const val MARKET_PACKAGE_OPPO = "com.heytap.market"
const val MARKET_WEBSITE_HUAWEI = "https://appgallery.huawei.com/app/C5683"
const val MARKET_WEBSITE_HONOR = ""
const val MARKET_WEBSITE_XIAOMI = "https://app.mi.com/details?id=com.tencent.mm"
const val MARKET_WEBSITE_VIVO = "https://h5.appstore.vivo.com.cn/#/details?appId=40413&frompage=searchResultApp"
const val MARKET_WEBSITE_OPPO = "https://store.oppomobile.com"
fun openMarketByPackage(packageName: String) {
val uri = Uri.parse("market://details?id=$packageName")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
getAppContext().startActivity(intent)
}
fun openMarketByChannel(channel: String) {
openMarketByChannel(channel, getAppContext().packageName)
}
fun openMarketByChannel(channel: String, packageName: String) {
val marketPackage = getMarketPackage(channel)
if (!isPackageInstalled(marketPackage)) {
openWebMarketByChannel(channel)
return
}
val context = getAppContext()
val uri = Uri.parse("market://details?id=$packageName")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setPackage(marketPackage)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
fun openWebMarketByChannel(channel: String) {
val context = getAppContext()
val website = getMarketWebsite(channel)
val uri = Uri.parse(website)
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
fun getMarketPackage(channel: String): String {
return when (channel) {
MARKET_CHANNEL_HUAWEI -> MARKET_PACKAGE_HUAWEI
MARKET_CHANNEL_HONOR -> MARKET_PACKAGE_HONOR
MARKET_CHANNEL_XIAOMI -> MARKET_PACKAGE_XIAOMI
MARKET_CHANNEL_VIVO -> MARKET_PACKAGE_VIVO
MARKET_CHANNEL_OPPO -> MARKET_PACKAGE_OPPO
else -> ""
}
}
fun getMarketWebsite(channel: String): String {
return when (channel) {
MARKET_CHANNEL_HUAWEI -> MARKET_WEBSITE_HUAWEI
MARKET_CHANNEL_HONOR -> MARKET_WEBSITE_HONOR
MARKET_CHANNEL_XIAOMI -> MARKET_WEBSITE_XIAOMI
MARKET_CHANNEL_VIVO -> MARKET_WEBSITE_VIVO
MARKET_CHANNEL_OPPO -> MARKET_WEBSITE_OPPO
else -> ""
}
}
fun isPackageInstalled(packageName: String): Boolean {
try {
getAppContext().packageManager.getApplicationInfo(packageName, 0)
return true
} catch (e: Throwable) {
return false
}
}
}