Android 跳转至各大应用商店应用详情页

2024-09-07 09:28

本文主要是介绍Android 跳转至各大应用商店应用详情页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

测试通过机型品牌:
华为、小米、红米、OPPO、一加、Realme、VIVO、IQOO、荣耀、魅族、三星

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import com.joyssom.common.utils.ZLog;public class MarketUtils {private static MarketUtils tools;private static final String schemaUrl = "market://details?id=";public static MarketUtils getTools() {if (null == tools) {tools = new MarketUtils();}return tools;}/***/* 不指定包名/* @param mContext*/public boolean startMarket(Context mContext) {String packageName = mContext.getPackageName();//得到包名return startMarket(mContext, packageName);}/*** /* 指定包名* <p>* /** <p>* /* @param mContext* <p>* /* @param packageName*/public boolean startMarket(Context mContext, String packageName) {try {String deviceBrand = getDeviceBrand();//获得手机厂商//根据厂商获取对应市场的包名String brandName = deviceBrand.toUpperCase();//大写if (TextUtils.isEmpty(brandName)) {ZLog.e("没有读取到手机厂商~~");return false;}String marketPackageName = getBrandName(brandName);ZLog.d("marketPackageName:" + marketPackageName);ZLog.d("brandName:" + brandName);ZLog.d("deviceBrand:" + deviceBrand);if (null == marketPackageName || "".equals(marketPackageName)) {return false;}if (marketPackageName.equals(MarketUtils.PACKAGE_NAME.SAMSUNG_PACKAGE_NAME)) {//三星特殊处理Uri uri = Uri.parse("http://www.samsungapps.com/appquery/appDetail.as?appId=" + packageName);Intent goToMarket = new Intent();goToMarket.setClassName("com.sec.android.app.samsungapps", "com.sec.android.app.samsungapps.Main");goToMarket.setData(uri);try {mContext.startActivity(goToMarket);return true;} catch (ActivityNotFoundException e) {return false;}} else {return startMarket(mContext, packageName, marketPackageName);}} catch (ActivityNotFoundException anf) {ZLog.e("要跳转的应用市场不存在!" + anf.getMessage());} catch (Exception e) {ZLog.e("其他错误:" + e.getMessage());}return false;}/***/* 指定包名,指定市场/* @param mContext/* @param packageName/* @param marketPackageName*/public boolean startMarket(Context mContext, String packageName, String marketPackageName) {try {openMarket(mContext, packageName, marketPackageName);return true;} catch (ActivityNotFoundException anf) {ZLog.e("要跳转的应用市场不存在!" + anf.getMessage());return false;} catch (Exception e) {ZLog.e("其他错误:" + e.getMessage());return false;}}/***/* 打开应用市场/* @param mContext/* @param packageName/* @param marketPackageName*/private boolean openMarket(Context mContext, String packageName, String marketPackageName) {try {Uri uri = Uri.parse(schemaUrl + packageName);Intent intent = new Intent(Intent.ACTION_VIEW, uri);intent.setPackage(marketPackageName);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mContext.startActivity(intent);return true;} catch (ActivityNotFoundException anf) {ZLog.e("要跳转的应用市场不存在!" + anf.getMessage());return false;} catch (Exception e) {ZLog.e("其他错误:" + e.getMessage());return false;}}/***/* 检测是否是应用宝或者是百度市场/* @param mContext/* @param packageName/* @return*/private boolean isCheckBaiduOrYYB(Context mContext, String packageName) {boolean installed = isInstalled(packageName, mContext);return installed;}/****/* 检查APP是否安装成功/* @param packageName/* @param context/* @return*/private boolean isInstalled(String packageName, Context context) {if ("".equals(packageName) || packageName.length() <= 0) {return false;}PackageInfo packageInfo;try {packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);} catch (PackageManager.NameNotFoundException e) {packageInfo = null;}if (packageInfo == null) {return false;} else {return true;}}private String getBrandName(String brandName) {if (BRAND.HUAWEI_BRAND.equals(brandName)) {//华为return PACKAGE_NAME.HUAWEI_PACKAGE_NAME;} else if (BRAND.OPPO_BRAND.equals(brandName)) {//opporeturn PACKAGE_NAME.OPPO_PACKAGE_NAME;} else if (BRAND.REALME_BRAND.equals(brandName)) {//realmereturn PACKAGE_NAME.OPPO_PACKAGE_NAME;} else if (BRAND.VIVO_BRAND.equals(brandName)) {//vivoreturn PACKAGE_NAME.VIVO_PACKAGE_NAME;} else if (BRAND.XIAOMI_BRAND.equals(brandName)) {//小米return PACKAGE_NAME.XIAOMI_PACKAGE_NAME;} else if (BRAND.REDMI_BRAND.equals(brandName)) {//红米return PACKAGE_NAME.XIAOMI_PACKAGE_NAME;} else if (BRAND.LENOVO_BRAND.equals(brandName)) {//联想return PACKAGE_NAME.LIANXIANG_PACKAGE_NAME;} else if (BRAND.MEIZU_BRAND.equals(brandName)) {//魅族return PACKAGE_NAME.MEIZU_PACKAGE_NAME;} else if (BRAND.HONOR_BRAND.equals(brandName)) {//荣耀return PACKAGE_NAME.HONOR_PACKAGE_NAME;} else if (BRAND.ZTE_BRAND.equals(brandName)) {//ztereturn PACKAGE_NAME.ZTE_PACKAGE_NAME;} else if (BRAND.NIUBIA_BRAND.equals(brandName)) {//努比亚return PACKAGE_NAME.NIUBIA_PACKAGE_NAME;} else if (BRAND.ONE_PLUS_BRAND.equals(brandName)) {//OnePlusreturn PACKAGE_NAME.OPPO_PACKAGE_NAME;} else if (BRAND.SONY_BRAND.equals(brandName)) {//索尼return PACKAGE_NAME.GOOGLE_PACKAGE_NAME;} else if (BRAND.SAMSUNG_BRAND.equals(brandName)) {return PACKAGE_NAME.SAMSUNG_PACKAGE_NAME;} else if (BRAND.GOOGLE_BRAND.equals(brandName)) {//googlereturn PACKAGE_NAME.GOOGLE_PACKAGE_NAME;}return "";}/*** /* 获取手机厂商*/private String getDeviceBrand() {return android.os.Build.BRAND;}public static class BRAND {public static final String HUAWEI_BRAND = "HUAWEI";//HUAWEI_PACKAGE_NAMEpublic static final String HONOR_BRAND = "HONOR";//HUAWEI_PACKAGE_NAMEpublic static final String OPPO_BRAND = "OPPO";//OPPO_PACKAGE_NAMEpublic static final String REALME_BRAND = "REALME";//OPPO_PACKAGE_NAMEpublic static final String MEIZU_BRAND = "MEIZU";//MEIZU_PACKAGE_NAMEpublic static final String VIVO_BRAND = "VIVO";//VIVO_PACKAGE_NAMEpublic static final String XIAOMI_BRAND = "XIAOMI";//XIAOMI_PACKAGE_NAMEpublic static final String REDMI_BRAND = "REDMI";//XIAOMI_PACKAGE_NAMEpublic static final String LENOVO_BRAND = "LENOVO";//LIANXIANG_PACKAGE_NAME //Lenovopublic static final String ZTE_BRAND = "ZTE";//ZTE_PACKAGE_NAMEpublic static final String XIAOLAJIAO_BRAND = "XIAOLAJIAO";//ZHUOYI_PACKAGE_NAMEpublic static final String QH360_BRAND = "360";//QH360_PACKAGE_NAMEpublic static final String NIUBIA_BRAND = "NUBIA";//NIUBIA_PACKAGE_NAMEpublic static final String ONE_PLUS_BRAND = "ONEPLUS";//OPPO_PACKAGE_NAMEpublic static final String MEITU_BRAND = "MEITU";//MEITU_PACKAGE_NAMEpublic static final String SONY_BRAND = "SONY";//GOOGLE_PACKAGE_NAMEpublic static final String GOOGLE_BRAND = "GOOGLE";//GOOGLE_PACKAGE_NAMEpublic static final String HTC_BRAND = "HTC";//未知应用商店包名public static final String ZUK_BRAND = "ZUK";//未知应用商店包名public static final String SAMSUNG_BRAND = "SAMSUNG";//未知应用商店包名}/** Redmi*//*** /* 华为,oppo,vivo,小米,360,联想,魅族,安智,百度,阿里,应用宝,goog,豌豆荚,pp助手**/public static class PACKAGE_NAME {public static final String OPPO_PACKAGE_NAME = "com.heytap.market";//oppopublic static final String VIVO_PACKAGE_NAME = "com.bbk.appstore";//vivopublic static final String HUAWEI_PACKAGE_NAME = "com.huawei.appmarket";//华为public static final String HONOR_PACKAGE_NAME = "com.hihonor.appmarket";//荣耀public static final String XIAOMI_PACKAGE_NAME = "com.xiaomi.market";//小米public static final String MEIZU_PACKAGE_NAME = "com.meizu.mstore";//,魅族public static final String LIANXIANG_PACKAGE_NAME = "com.lenovo.leos.appstore";//联想public static final String ZTE_PACKAGE_NAME = "zte.com.market";//ztepublic static final String GOOGLE_PACKAGE_NAME = "com.android.vending";//googlepublic static final String NIUBIA_PACKAGE_NAME = "com.nubia.neostore";//努比亚public static final String SAMSUNG_PACKAGE_NAME = "com.sec.android.app.samsungapps";}/*** /* 启动到应用商店app详情界面* <p>* /* @param appPkg 目标App的包名* <p>* /* @param marketPkg 应用商店包名 ,如果为"" 则由系统弹出应用商店* <p>* /* 列表供用户选择,否则调转到目标市场的应用详情界面,某些应用商店可能会失败*/public static void launchAppDetail(Context context, String appPkg, String marketPkg) {try {if (TextUtils.isEmpty(appPkg)) return;Uri uri = Uri.parse("market://details?id=" + appPkg);Intent intent = new Intent(Intent.ACTION_VIEW, uri);if (!TextUtils.isEmpty(marketPkg)) {intent.setPackage(marketPkg);}intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent);} catch (Exception e) {e.printStackTrace();}}
}

这篇关于Android 跳转至各大应用商店应用详情页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1144688

相关文章

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使

Python循环缓冲区的应用详解

《Python循环缓冲区的应用详解》循环缓冲区是一个线性缓冲区,逻辑上被视为一个循环的结构,本文主要为大家介绍了Python中循环缓冲区的相关应用,有兴趣的小伙伴可以了解一下... 目录什么是循环缓冲区循环缓冲区的结构python中的循环缓冲区实现运行循环缓冲区循环缓冲区的优势应用案例Python中的实现库

SpringBoot整合MybatisPlus的基本应用指南

《SpringBoot整合MybatisPlus的基本应用指南》MyBatis-Plus,简称MP,是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,下面小编就来和大家介绍一下... 目录一、MyBATisPlus简介二、SpringBoot整合MybatisPlus1、创建数据库和

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

Java逻辑运算符之&&、|| 与&、 |的区别及应用

《Java逻辑运算符之&&、||与&、|的区别及应用》:本文主要介绍Java逻辑运算符之&&、||与&、|的区别及应用的相关资料,分别是&&、||与&、|,并探讨了它们在不同应用场景中... 目录前言一、基本概念与运算符介绍二、短路与与非短路与:&& 与 & 的区别1. &&:短路与(AND)2. &:非短