DeepLink的实现原理

前言

之前我们又是看源码又是研究动画,今天分享一个比较简单的技术点:DeepLink。

DeepLink,深度链接技术,主要应用场景是通过Web页面直接调用Android原生app,并且把需要的参数通过Uri的形式,直接传递给app,节省用户的注册成本。简单的介绍DeepLink概念之后,我们看一个实际的例子:

朋友通过京东分享给我一个购物链接:

于是我通过微信打开了这条链接:

4731536205741_.pic.jpg

在微信中打开这个网址链接,提示我打开京东app,如果我点击了允许,就会打开我手机中的京东app,并且跳转到这个商品的详情页:

4721536205740_.pic.jpg

通过这种形式,我可以快速的找到需要查看的商品,并且完成购买相关的操作。是不是非常方便,这就是DeepLink。

正文

这么流弊的DeepLink是不是非常的难?其实DeepLink的基本实现是简单到不可思议,他的核心思想实际上是Android的隐式启动。我们平时的隐式启动主要是通过Action和Category配合启动指定类型的Activity:


      
           
           
      

val intent = Intent("com.lzp.deeplinkdemo.SECOND")
intent.addCategory(Intent.CATEGORY_DEFAULT)
startActivity(intent)

除了action和category,还有一种隐式启动的用法是配置data:


scheme:协议类型,我们可以自定义,一般是项目或公司缩写,String

host:域名地址,String

port:端口,int。

path:访问的路径,String

pathPrefix:访问的路径的前缀,String

pathPattern:访问路径的匹配格式,相对于path和pathPrefix更为灵活,String

mimeType:资源类型,例如常见的:video/*, image/png, text/plain。

通过这几个配置项,我们发现data实际上为当前的页面绑定了一个Uri地址,这样就可以通过Uri直接打开这个Activity。

复习一下Uri的结构:

:// : / [ | | ]

示例:https://zhidao.baidu.com/question/2012197558423339788.html

scheme和host不可缺省,否则配置无效;path,pathPrefix,pathPattern一般指定一个就可以了,pathPattern与host不可同时使用;mimeType可以不设置,如果设置了,跳转的时候必须加上mimeType,否则不能匹配到Activity。

现在我们修改SecondActivity的intent-filer:



        
            
            
        

                      

            

        

打开SecondActivity:

val intent = Intent()
intent.setDataAndType(Uri.parse("lzp://demo:8888/second"), "text/plain")
startActivity(intent)

现在在App中已经可以打开页面了,那么用web能不能正常打开呢?首先配置MainActivity的intent-filter:


        
            
            
        

        
            
        


Web需要打开url链接,所以我们不需要配置mimeType,

手写一个简单的Html页面:




  
  
  
  
  
  


打开main

Html页面添加了一个链接,点击打开lzp://demo/main这个地址。把html导入到手机中,用浏览器打开,点击“打开app”,毫无反应!!!

没错,如果只是配置了data,Web还是没办法通过url地址打开我们的Activity,那怎么解决这个问题呢?

/**
     * Activities that can be safely invoked from a browser must support this
     * category.  For example, if the user is viewing a web page or an e-mail
     * and clicks on a link in the text, the Intent generated execute that
     * link will require the BROWSABLE category, so that only activities
     * supporting this category will be considered as possible actions.  By
     * supporting this category, you are promising that there is nothing
     * damaging (without user intervention) that can happen by invoking any
     * matching Intent.
     */
    @SdkConstant(SdkConstantType.INTENT_CATEGORY)
    public static final String CATEGORY_BROWSABLE = "android.intent.category.BROWSABLE";

我们还需要配置


从官方的注释上写明:需要浏览器打开Activity,需要设置这个分类。例如邮件,只有设置了这个分类的Activity才会考虑被打开。加上这个配置后,再次点击看看有没有效果。

如果你真的亲自尝试了,你会发现还是没有效果。这个时候我们需要回顾一下action和category的用法:

首先需要尝试匹配action,action匹配成功了之后,才会继续匹配设置的category,所以单独匹配category是没有任何效果的。

因为我们要打开的仅仅是一个页面,所以我们设置

/**
     * Activity Action: Display the data to the user.  This is the most common
     * action performed on data -- it is the generic action you can use on
     * a piece of data to get the most reasonable thing to occur.  For example,
     * when used on a contacts entry it will view the entry; when used on a
     * mailto: URI it will bring up a compose window filled with the information
     * supplied by the URI; when used with a tel: URI it will invoke the
     * dialer.
     * 

Input: {@link #getData} is URI from which to retrieve data. *

Output: nothing. */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String ACTION_VIEW = "android.intent.action.VIEW";

官方的注释说明ACTION_VIEW表示展示数据的页面,系统默认的Action就是ACTION_VIEW。添加上ACTION_VIEW,再次点击打开app。

还是不行,但是跟之前不同的是,这次出现了启动app的提示窗口,但是app却闪退了,看一下崩溃日志:

09-06 14:35:15.459 1216-3270/? W/IntentResolver: resolveIntent failed: found match, but none with CATEGORY_DEFAULT
09-06 14:35:15.473 26708-26708/? E/AKAD: thread:Thread[main,5,main]
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=lzp://demo/main?id=111 flg=0x10000000 (has extras) }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
        at android.app.Activity.startActivityIfNeeded(Activity.java:4420)
        at android.app.Activity.startActivityIfNeeded(Activity.java:4367)
        at org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl$3.onClick(ExternalNavigationDelegateImpl.java:239)
        at com.qihoo.browser.dialog.CustomDialog$3.onClick(CustomDialog.java:274)
        at android.view.View.performClick(View.java:5267)
        at android.view.View$PerformClick.run(View.java:21249)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

日志上写的很明确,虽然找到了匹配的页面,但是没有设置CATEGORY_DEFAULT。看来Web通过url来打开链接,必须要求设置CATEGORY_DEFAULT,添加上后,看一下我们完整的xml配置:


        
            
            
        

        
            
            
            
            
        

最后看一下效果:


gif_20180906_151341.gif

那么如何在通过url给app传递参数呢?要实现这个也很简单,首先我们知道要想给url添加参数,直接在url后拼接key=value就可以了,例如:

http://www.baidu.com/s?wd=android

其中wd=android就是我们要添加的参数,现在假设我们需要为Activity传递一个参数id,我们就可以修改uri为:

lzp://demo/main?id=111

客户端接收key为id的参数的方法:

if (intent != null && intent.data != null) {
       Log.e("lzp", intent.data.getQueryParameter("id"))
}

如果只是接收参数的话,客户端不需要进行任何修改,但是这里有一种情况,如果我们Activity必须传递id,如果不传递id不允许跳转怎么办呢?我们有两种办法解决这个问题:

1、在刚才的if语句增加else判断,当参数为空的时候,进行finish操作。

2、通过pathPattern,通过通配符设置必须有参数。

我们看一下第二种的实现方式:


之前已经说过,pathPattern不能和host同时使用,所以我们只能删除host,pathPattern匹配的是整个Uri,这样我们还可以指定多个参数。但是AndroidManifest.xml会报错,我们忽略就可以了

总结

其实DeepLink的实现原理就是这么简单,只是我们对于隐式启动理解的不够。是不是也想给自己的App加上DeepLink呢?赶快尝试一下吧~

你可能感兴趣的:(DeepLink的实现原理)