【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
        }
    }
}
相关推荐
Dnelic-1 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen3 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年10 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿13 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神14 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛14 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法15 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter16 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快17 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl17 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5