h5页面唤起本地APP

本来没有写博客的习惯,但是发现,记忆力现在差好多,不写下来就忘了,主要也是帮助其他人更好的实现功能。因为自己百度找了很多,但是每个人的项目都不同,实现的效果也不一样。我们知道,分享一个产品详情页到微信朋友圈,打开直接跳到产品详情页(这里忽略前端同学的处理过程,只写Android端需要做的)。唤起APP,需要uri sheme.我们可以在程序的入口activity中加入以下代码。差点忘了,不要用浏览器直接输入地址,这样做是打不开APP的,我自己简单的了个html,放到tomcat上部署一下,这样就可以了。


<html>  
<head>  
<meta charset="UTF-8">  
<title>mpos</title>  
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0">  
<meta name="imagemode" content="force">  
<meta name="apple-mobile-web-app-capable" content="yes">  
<meta name="apple-mobile-web-app-status-bar-style" content="black">  
<meta name="format-detection" content="telephone=no">  
</head>  
<body>  
<h1>H5打开APP</h1>  
<a href="ykly://detail/visa?136"> Open APP </a>  
</body>  
</html> 

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="ykly"
        android:host="detail"/>
</intent-filter>
然后在入口activity中,我的项目是欢迎界面。获取数据,比如产品详情页需要传产品类型,产品id,那么这时候就可以获取到。

Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {//如果是分享过来的要走这个intent跳到详情页
    Uri uri = intent.getData();
    if (uri != null) {
        String host = uri.getHost();
        String dataString = intent.getDataString();
        String id = uri.getQueryParameter("id");
        String path = uri.getPath();
        String path1 = uri.getEncodedPath();
        String productId = uri.getQuery();
        Log.d("分享", "host:" + host);
        Log.d("分享", "dataString:" + dataString);
        Log.d("分享", "id:" + id);
        Log.d("分享", "type:" + path);
        Log.d("分享", "path1:" + path1);
        Log.d("分享", "productId:" + productId);
        String[] types = path.split("/");
        String type = types[1];
        if (type.equals("tour")) {
            Intent intent11 = new Intent(this, MainActivity.class);
            intent11.setFlags(
                    Intent.FLAG_ACTIVITY_NO_ANIMATION);//acitivity设置成无动画,这样详情页后退时就退到了主页
            startActivity(intent11);
            Intent intent1 = new Intent(this, GroupTourDetailActivity.class);
            intent1.putExtra("productId", intent.getData().getQuery());
            intent1.putExtra("Category", "GTY");
            startActivity(intent1);
        } else if (type.equals("visa")) {
            Intent intent22 = new Intent(this, MainActivity.class);
            intent22.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent22);
            Intent intent2 = new Intent(this, VisaDetailActivity.class);
            intent2.putExtra("visaId", intent.getData().getQuery());
            intent2.putExtra("Category", "VISA");
            startActivity(intent2);
        } else if (type.equals("ship")) {
            Intent intent33 = new Intent(this, MainActivity.class);
            intent33.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent33);
            Intent intent3 = new Intent(this, CruiseShipDetailActivity.class);
            intent3.putExtra("product_id", intent.getData().getQuery());
            intent3.putExtra("Category", "YL");
            startActivity(intent3);
        }
    }
} else {
    initSystem();
    MyApp.flag = 0;
    //引导页
    pre = getSharedPreferences("count", MODE_PRIVATE);
}

if里面是分享页面过来的。else是正常打开APP。具体逻辑要根据自己项目写。这样,点击后退键就可以退到首页。实现的效果还是不错的。代码已经写的相当详细了。也许还有更好的实现方式,欢迎大家推荐。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值