最近接到一个需求,开启启动显示自有广告图片,点击跳转到淘宝、天猫、京东商品详情页,实现代码如下:
/**
* 跳转到淘宝
* @param url 淘宝商品链接地址
*/
private void toTaoBao(String url){
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri uri = Uri.parse(url);
intent.setData(uri);
intent.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
/**第二种方式
String id = getUrlParam(url,"id");
String taobao_url= "taobao://item.taobao.com/item.htm?id="+id+"";
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(taobao_url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); */
}
/**
* 跳转到天猫
* @param url 天猫商品链接地址
*/
private void toTianMao(String url){
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
String id = getUrlParam(url,"id");
String tmall_url = "tmall://tmallclient/?{\"action\":\"item:id="+id+"\"}";
Uri uri = Uri.parse(tmall_url);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
/**
* 跳转到京东
* @param url 京东商品链接地址
*/
private void toJingDong(String url){
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
String startHtml = url.substring(0, url.indexOf(".html"));
String id = startHtml.substring(startHtml.lastIndexOf("/")+1);
String jd_url = "openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%22"+id+"%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D";
//两种url拼接都可以
//String jd_url = "openApp.jdMobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\""+id+"\",\"sourceType\":\"JSHOP_SOURCE_TYPE\",\"sourceValue\":\"JSHOP_SOURCE_VALUE\"}";
Uri uri = Uri.parse(jd_url);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
/**
* 获取URL中的某个参数
*/
public static String getUrlParam(String url, String name) {
Uri uri = Uri.parse(url);
return uri.getQueryParameter(name);
}
ps:
1.如果不设置Intent.FLAG_ACTIVITY_NEW_TASK,现象是在本app上显示详情页面
2.天猫跳转有个问题,第一次跳转后退出商品详情页,返回到天猫首页。这个时候重新启动app,点击跳转只能到天猫首页不能到商品详情页(如果有小伙伴解决这个问题,望告知解决方式)
网友评论