Android 自定义scheme及多端唤起使用方法

前言

DeepLink,深度连接技术,相似于web开发领域不单单是经过连接打开一个界面,而是打开界面的某个具体内容。经常使用于web端唤起app时,传递参数直接打开肯定的界面,如经过京东的分享出去的商品详情页,实如今京东app中打开。html

在移动开发领域,是指app在处理特定的url时可以直接跳转到对应的内容页面或者触发特定的逻辑。这样能够在web端切换app时经过参数传递保留了当前的上下文状态,又能够借用web端的优点,更利于传播,可利用搜索引擎的索引,增长app的日活和下载量等。java

移动端实现deeplink因而就有了Universal Link、App Link、URL schemes等android

Android配置scheme

如配置WebActivity完整的打开连接为openapp://test:8000/detail,须要在AndroidManifest.xml配置web

<activity android:name=".WebActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <!--须要被js调起必须设置-->
        <category android:name="android.intent.category.BROWSABLE" />
        <!--协议部分-->
        <data android:host="test" android:path="/detail" android:port="8000" android:scheme="openapp" />
    </intent-filter>
</activity>
复制代码

协议部分:(类比于http://locahost:8080/home)app

  • android:scheme:协议名,相似于http
  • android:host:主机名,相似于locahost
  • android:port:端口名。相似于8080中的端口
  • android:path:路径名,相似于home
  • 还能够配置imei等等。结构为<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]

Android端调起

经过指定Intent的Action为Intent.ACTION_VIEW,传入解析的Uriui

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("openapp://test:8000/detail?title=电视影音&url=https://u.jd.com/1dfFwO"));
startActivity(intent);
复制代码

传递参数的方法跟web端同样,经过问号?分隔,参数名和值之间使用等号=链接,多个参数之间使用&拼接。搜索引擎

Android端参数接收

Uri uri = getIntent().getData();
if (uri != null) {
    // 完整的url信息
    String totalUrl = uri.toString();
    Log.i(TAG, "完整url: " + totalUrl);
    // 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);
    // 获取全部参数
    String query = uri.getQuery();
    Log.i(TAG, "query: " + query);
    //获取指定参数值
    String title = uri.getQueryParameter("title");
    Log.i(TAG, "title: " + title);
    String url = uri.getQueryParameter("url");
    Log.i(TAG, "url: " + url);
}
复制代码

唤起后能够看见打印的日志信息: {% asset_img 15705317238763.png%}url

拿到参数以后就能够进行后续的使用操做啦spa

web端唤起

直接当作一个普通的链接形式,直接跳转日志

window.location = "openapp://test:8000/detail?title=电视影音&url=https://u.jd.com/1dfFwO";  
复制代码

或者设置超连接等待用户点击

<a href="openapp://test:8000/detail?title=电视影音&url=https://u.jd.com/1dfFwO">在app中打开</a>
复制代码

原文连接

💡 更多好文欢迎关注个人公众号~

公众号
相关文章
相关标签/搜索