外部跳转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里面写大写!!!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,827评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,145评论 2 59
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 日常上图.png 日常安利自己的Github,如果有错误或者理解不正确的地方,麻烦告知我会及时更正。同时也非常欢迎...
    Xiao呆不糊涂阅读 12,184评论 3 61
  • 今天从郭林姐那里得知,高霞姐的丈夫田大哥确诊了,是肺癌晚期。听到消息的那一刻我慌了,心里不停地在问怎么办怎么办,高...
    美少女敏阅读 291评论 0 0