【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
        }
    }
}
相关推荐
张风捷特烈8 小时前
Flutter 伪3D绘制#03 | 轴测投影原理分析
android·flutter·canvas
omegayy11 小时前
Unity 2022.3.x部分Android设备播放视频黑屏问题
android·unity·视频播放·黑屏
mingqian_chu11 小时前
ubuntu中使用安卓模拟器
android·linux·ubuntu
自动花钱机11 小时前
Kotlin问题汇总
android·开发语言·kotlin
行墨14 小时前
Kotlin 主构造函数
android
前行的小黑炭14 小时前
Android从传统的XML转到Compose的变化:mutableStateOf、MutableStateFlow;有的使用by有的使用by remember
android·kotlin
_一条咸鱼_14 小时前
Android Compose 框架尺寸与密度深入剖析(五十五)
android
在狂风暴雨中奔跑14 小时前
使用AI开发Android界面
android·人工智能
行墨14 小时前
Kotlin 定义类与field关键
android
信徒_15 小时前
Mysql 在什么样的情况下会产生死锁?
android·数据库·mysql