在项目需求中有一个需要跳转到淘宝App主页的需求。
在这里简单列举下跳转淘宝的实现方式
1.项目集成了阿里百川SDK,使用SDK提供的API直接跳转,可以携带参数并跳转到具体页面
2.单纯的跳转,项目没有集成阿里百川SDK,需要自己查找淘宝对应的Activity和传递的参数。
跳转其他App的方式都可以2的方式去实现,找出对应App的包名,Activity名,参数列表。
这里就采用2的方式去实现吧,流程是这样的
1.使用PackageManager判断对应的app是否安装了
2.假若app安装了,Intent隐式并携带参数跳转
这里列出下主流App的一些包名:
instagram com.instagram.android
facebook com.facebook.katana
messenger com.facebook.orca
whatsapp com.whatsapp
Gmail com.google.android.gm
GoogleMap com.google.android.apps.maps
Allo com.google.android.apps.fireball
美团外卖 com.sankuai.meituan.takeoutnew
饿了么 me.ele
摩拜单车 com.mobike.mobikeapp
OFO so.ofo.labofo
今日头条 com.ss.android.article.news
新浪微博 com.sina.weibo
网易新闻 com.netease.newsreader.activity
快手 com.smile.gifmaker
知乎 com.zhihu.android
虎牙直播 com.duowan.kiwi
映客直播 com.meelive.ingkee
秒拍 com.yixia.videoeditor
美图秀秀 com.mt.mtxx.mtxx
美颜相机 com.meitu.meiyancamera
携程 ctrip.android.view
陌陌 com.immomo.momo
优酷 com.youku.phone
爱奇艺 com.qiyi.video
滴滴出行 com.sdu.didi.psnger
支付宝 com.eg.android.AlipayGphone
淘宝 com.taobao.taobao
京东 com.jingdong.app.mall
大众点评 com.dianping.v1
搜狗输入法 com.sohu.inputmethod.sogou
百度地图 com.baidu.BaiduMap
高德地图 com.autonavi.minimap
简书 com.jianshu.haruki
得到 com.luojilab.player
喜马拉雅 com.ximalaya.ting.android
微信 com.tencent.mm
QQ com.tencent.mobileqq
QQ音乐 com.tencent.qqmusic
微信读书 com.tencent.weread
QQ阅读 com.qq.reader
唱吧 com.changba
网易云音乐 com.netease.cloudmusic
钉钉 com.alibaba.android.rimet
至于这些包名的获取,可以用 adb shell dumpsys activity 输出当前Activity栈的指令来获取(前提安装了软件,而且打开了软件,之后在命令台输出。= =)
但是百度上面有很多已经现成的包名,因为经过那么多开发者的分享,例如上面列出来的那么多包名。
有了包名之后如何跳转呢?
使用Intent的隐式跳转就ok了~这里直接上代码
PackageUtils:检查包是否安装
ThirdAppNavigator:第三方App跳转类
PackageUtils.java
/**
* @Author:Rayman
* @Date:2018/12/22
* @Description:Android包管理器
*/
public class PackageUtils {
/**
* description:国外热门App
**/
public static final String INSTAGRAM = "com.instagram.android";
public static final String FACE_BOOK = "com.facebook.katana";
public static final String MESSENGER = "com.facebook.orca";
public static final String WHATS_APP = "com.whatsapp";
public static final String GMAIL = "com.google.android.gm";
public static final String GOOGLE_MAP = "com.google.android.apps.maps";
public static final String ALLO = "com.google.android.apps.fireball";
/**
* description:国内热门App
**/
public static final String MEITUAN_WAIMAI = "com.sankuai.meituan.takeoutnew";
public static final String E_LE_ME = "me.ele";
public static final String MO_BAI = "com.mobike.mobikeapp";
public static final String OFO = "so.ofo.labofo";
public static final String JIN_RI_TOU_TIAO = "com.ss.android.article.news";
public static final String SINA_WEI_BO = "com.sina.weibo";
public static final String WANG_YI_XIN_WEN = "com.netease.newsreader.activity";
public static final String KUAI_SHOU = "com.smile.gifmaker";
public static final String ZHI_HU = "com.zhihu.android";
public static final String HU_YA_ZHI_BO = "com.duowan.kiwi";
public static final String YING_KE_ZHI_BO = "com.meelive.ingkee";
public static final String MIAO_PAI = "com.yixia.videoeditor";
public static final String MEI_TU_XIU_XIU = "com.mt.mtxx.mtxx";
public static final String MEI_YAN_XIANG_JI = "com.meitu.meiyancamera";
public static final String XIE_CHENG = "ctrip.android.view";
public static final String MO_MO = "com.immomo.momo";
public static final String YOU_KU = "com.youku.phone";
public static final String AI_QI_YI = "com.qiyi.video";
public static final String DI_DI = "com.sdu.didi.psnger";
public static final String ZHI_FU_BAO = "com.eg.android.AlipayGphone";
public static final String TAO_BAO = "com.taobao.taobao";
public static final String JING_DONG = "com.jingdong.app.mall";
public static final String DA_ZONG_DIAN_PING = "com.dianping.v1";
public static final String JIAN_SHU = "com.jianshu.haruki";
public static final String BAI_DU_DI_TU = "com.baidu.BaiduMap";
public static final String GAO_DE_DI_TU = "com.autonavi.minimap";
public static final String WEI_XIN = "com.tencent.mm";
public static final String QQ = "com.tencent.mobileqq";
public static boolean isAppInstalled(Context context, String uri) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
}
ThirdAppNavigator.java
/**
* @Author:Rayman
* @Date:2018/12/23
* @Description:第三方App跳转
*/
public class ThirdAppNavigator {
public static void startTaobaoApp() {
if (PackageUtils.isAppInstalled(ApplicationUtils.getApplication(), PackageUtils.TAO_BAO)) {
Intent intent = new Intent();
intent.setAction("Android.intent.action.VIEW");
intent.setClassName("com.taobao.taobao", "com.taobao.tao.welcome.Welcome");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ApplicationUtils.getApplication().startActivity(intent);
} else {
ToastHelper.showToast("请先安装淘宝客户端");
}
}
public static void startTaobaoMainActivity() {
if (PackageUtils.isAppInstalled(ApplicationUtils.getApplication(), PackageUtils.TAO_BAO)) {
Intent intent = new Intent();
intent.setAction("Android.intent.action.VIEW");
intent.setClassName("com.taobao.taobao", "com.taobao.tao.homepage.MainActivity3");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ApplicationUtils.getApplication().startActivity(intent);
} else {
ToastHelper.showToast("请先安装淘宝客户端");
}
}
}
PS:跳转的时候需要注意的几点
1.跳转的时候需要判断App是否安装
2.跳转的className,可以通过上面的 adb shell dumpsys activity 分析得到。找到对应的Activity名
3.跳转的参数。这个的话能百度就百度吧,因为不同业务流程需要不同的App,这里不列举了。
4.跳转的Flags需要设置成Intent.FLAG_ACTIVITY_NEW_TASK