通过Scheme协议打开APP界面

一、应用场景

用户在访问我们的网页时,判断出这个用户手机上是否安装了我们的App,如果安装了则直接从网页上打开APP,否则就引导用户前往下载,从而形成一个推广上的闭环。这里只针对从网页端打开本地APP。

二、APP端配置

<activity
    android:name=".ui.activity.ZMCertTestActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:screenOrientation="portrait">
    <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="scheme1"
            android:host="host1"
            android:path="/path1"
            android:port="8080" />
    </intent-filter>
</activity>

WEB端通过调用“scheme1://host1:8080/path1?query1=1&query2=true“便能打开这个Activity。其中scheme和host是必须的,另外的看需求。

2.1 APP端获取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端打开

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <a href="scheme1://host1:8080/path1?query1=1&query2=true">打开APP</a>
</body>
<html>

核心就是一段Schema协议的URL,scheme1、host1是打开APP页面所必须的。传递的参数都可以在APP页面中获取到。

四、通过另一个APP打开

Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("scheme1://host1:8080/path1?query1=1&query2=true"));
startActivity(intent);

可以try catch一下,出现Exception说明手机没有安装想打开的APP。

五、后续步骤

通过Scheme协议打开APP时,又存在两种情况,即APP当前是否已经打开了。当然,我们希望能够做到APP已打开的话便直接跳转到当前已经打开的页面,没有打开则开启APP。具体实现请见第三方唤醒APP以及四种启动模式的思考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,085评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,157评论 25 708
  • Apple 官方文档:《Apple Scheme URL Reference》翻译一篇。文虽短但里面还是有些有意思...
    从来吃不胖阅读 3,871评论 0 50
  • 我最讨厌骗子 可自己时常就是谎言的缔造者 自己莫名其妙地进入到困惑的阶段 我不知道自己想要的是什么 好吧,这是一个...
    alim0922阅读 266评论 0 2
  • 万古开新陈子昂 风骨俱发废齐梁 翰墨写尽上官体 幽州一阙启盛唐
    庸晓阅读 252评论 2 3

友情链接更多精彩内容