外部跳转APP

发布时间:2019-06-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了外部跳转APP脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

需求

广告推广华为微服务;通过外部网页或者卡片跳转到我们的app指定界面。如果app已经存在打开app,app不存在跳转下载界面。

APP配置

       <activity
            android:name=".LauncherActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--start-->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!--scheme host 必填-->
                <!--path port 根据需求添加-->
                <data
                    android:host="host"
                    android:scheme="scheme" />
            </intent-filter>
            <!--end-->
        </activity>

scheme 、host 是必填项
android:path="/path"
android:port="8080"
根据需求进行添加

获取URL scheme中的值

        Uri uri = getIntent().getData();
        if (uri != null) {
            // 完整的url信息
            String url = uri.toString();
            Log.i(TAG, "url:" + uri);

            // scheme部分
            String scheme = uri.getScheme();
            Log.i(TAG, "scheme:" + scheme);

            // host部分
            String host = uri.getHost();
            Log.i(TAG, "host:" + host);

            // port部分
            int port = uri.getPort();
            Log.i(TAG, "port:" + port);

            // 访问路劲
            String path = uri.getPath();
            Log.i(TAG, "path:" + path);

            List<String> pathSegments = uri.getPathSegments();

            // Query部分
            String query = uri.getQuery();
            Log.i(TAG, "query:" + query);

            //获取指定参数值
            String success = uri.getQueryParameter("success");
            Log.i(TAG, "success:" + success);
        }
}

通过web打开,核心参数scheme 和 host

1、创建一个htML,把这个粘贴扔进去。如果安装了app就可以打开

<a href="scheme://host">打开app</a>

2、这个可以配置下载链接,如果APP未安装会跳转下载链接地址
(根据需要自己修改,网上找的例子)

<a id="call-app" href="javascript:;" > Start or Download </a><br/><br/>
        
        
    <script type="text/javascript">
 
            (function(){
 
        var ua = navigator.userAgent.toLowerCase();
 
        var t;
 
        var config = {
 
                /*scheme:必须*/
 
                scheme_IOS: 'scheme://host',
 
                scheme_Adr: 'scheme://host',
 
                download_url: 'http://a.app.qq.com/o/simple.jsp?pkgname=com.test',
 
                timeout: 600
 
        };
 
 
 
        function openclient() {
 
            var startTime = Date.now();
 
 
 
            var ifr = document.createElement('iframe');
 
 
 
            ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;
 
            ifr.style.display = 'none';
 
            document.body.appendChild(ifr);
 
 
 
            var t = setTimeout(function() {
 
                var endTime = Date.now();
 
 
 
                if (!startTime || endTime - startTime < config.timeout + 200) {
 
                    window.location = config.download_url;
 
                } else {
 
 
 
                }
 
            }, config.timeout);
 
 
 
            window.onblur = function() {
 
                clearTimeout(t);
 
            }
 
        }
 
        window.addEventListener("DOMContentLoaded", function(){
 
            document.getElementById("call-app").addEventListener('click',
 
                    openclient, false);
 
        }, false);
 
    })()
    </script>

通过另外一个app打开

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("scheme://host"));
startActivity(intent);

可以try catch一下,出现Exception说明手机没有安装想打开的APP,进行其他处理或者提示。

华为微服务

用到的是deeplink链接,使用原理也是scheme

遇坑

scheme | host 大小写问题(亲测实坑)

scheme host 在浏览器里是不分大小写的,会统一转为小写。
所以不要再scheme、host里面写大写!!!
所以不要再scheme、host里面写大写!!!
所以不要再scheme、host里面写大写!!!

脚本宝典总结

以上是脚本宝典为你收集整理的外部跳转APP全部内容,希望文章能够帮你解决外部跳转APP所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。