需求背景:
因为各种原因,需要打开手机自带的应用商店
核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import android.content.Context import android.content.Intent import android.net.Uri
object MarketUtils { const val XIAOMI_MARKET = "com.xiaomi.market" const val HUAWEI_MARKET = "com.huawei.appmarket" const val OPPO_MARKET = "com.oppo.market" const val OPPO_MARKET2 = "com.heytap.market" const val VIVO_MARKET = "com.bbk.appstore" const val MEIZU_MARKET = "com.meizu.mstore" const val YYB_MARKET = "com.tencent.android.qqdownloader"
fun startTargetMarket(context: Context, deepLink: String, packageName: String) { try { val uri = Uri.parse(deepLink) val intent = Intent(Intent.ACTION_VIEW, uri) intent.setPackage(packageName) context.startActivity(intent) } catch (e: Exception) { e.printStackTrace() val uri = Uri.parse(deepLink) val intent = Intent(Intent.ACTION_VIEW, uri) context.startActivity(intent) } }
private fun startTencentMarket(context: Context, deepLink: String) { val uri: Uri = Uri.parse(deepLink) val intent = Intent(Intent.ACTION_VIEW, uri) intent.setClassName( "com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity" ) context.startActivity(intent) } }
|
测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| private fun initView() { val deepLink = "market://details?id=com.taobao.taobao&ref=&caller=com.taobao.taobao&token=OWNjY2YyNGNiYzYwYTNkMCMxNjQ5NjQ2NTA0OTA5IzEjY29tLnRhb2Jhby50YW9iYW8jS29iZS8yNEAwZjY1NGI3ZGZlZWJlZWI2NTk3MTkyNWE4ZDFhZTc1Zg==&style=1&m=adapi_2049933&tk_con=eyJ0cmFja0lkIjoiN2VkOGI0ZWM2YjAwN2FmNjkzZDc0ODJkYTBlNzgyMDMiLCJkZXZpY2VJZCI6Ijg2Mzg5NDAzMjE1ODg3NyIsImFwcElkIjoiMjMzNCIsInZlcnNpb25JZCI6IjAiLCJlbnRlcklkIjoiMTQiLCJwYWdlSWQiOiIxMDAwMDEiLCJhYiI6IjFfMF85IiwiYWRJZCI6IjQwNzA2ODY5OCIsInQiOiIxNjQ5NjQ2NTA0OTA3IiwidiI6InYxIn0%3D&tk_ref=%7B%22adId%22%3A%22407068698%22%2C%22trackId%22%3A%227ed8b4ec6b007af693d7482da0e78203%22%7D" xiaomi_market.setOnClickListener { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.XIAOMI_MARKET) } huawei_market.setOnClickListener { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.HUAWEI_MARKET) } oppo_market.setOnClickListener { if (android.os.Build.VERSION.SDK_INT >= 28) { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.OPPO_MARKET2) } else { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.OPPO_MARKET) } } vivo_market.setOnClickListener { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.VIVO_MARKET) } meizu_market.setOnClickListener { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.MEIZU_MARKET) } yyb_market.setOnClickListener { MarketUtils.startTargetMarket(this, deepLink, MarketUtils.YYB_MARKET) } }
|