【Android】安卓打开指定厂商的应用市场

解决思路
  • 根据厂商名,获取指定应用市场包名,和网页版地址
  • 如果本地已安装应用市场软件,则跳转至应用市场下载
  • 如果本地未安装应用市场软件,则跳转至网页版应用市场下载
  • 如果未提供网页版应用市场平台,则跳转至该应用市场的下载地址
  • 以上条件均不满足,提示无法找到对应的应用市场
实现代码
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
        }
    }
}
相关推荐
木头没有瓜1 分钟前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp
键盘侠0073 分钟前
springboot 上传图片 转存成webp
android·spring boot·okhttp
江上清风山间明月30 分钟前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
Crossoads2 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言
sunphp开发者3 小时前
黑客攻击网站,篡改首页问题排查修复
android·js
我又来搬代码了3 小时前
【Android Studio】创建新项目遇到的一些问题
android·ide·android studio
ggs_and_ddu8 小时前
Android--java实现手机亮度控制
android·java·智能手机
zhangphil13 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl014 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-14 小时前
PHP爬虫类的并发与多线程处理技巧
android