Android应用链接 与深层链接

一个Activity除了可以通过startActivity的方法启动之外,还可以通过scheme协议来启动

说明
一个activity通过scheme协议 方法拉起,也就是深层链接。

当点击的链接或程序化请求调用网页 URI intent 时,Android 系统会按顺序尝试执行以下每项操作,直到请求成功为止:

  1. 如果用户指定了可以处理该 URI 的首选应用,则打开此应用。
    2.打开唯一可以处理该 URI 的应用。
    3.允许用户从对话框中选择应用。
 <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <data android:scheme="bpp"
                    android:host="hxd.app"/>
                <category android:name="android.intent.category.DEFAULT" />    // 用于响应 隐式 intent
                <category android:name="android.intent.category.BROWSABLE" />   //配置这个允许 从网络浏览器中访问 intent 过滤器
                <action android:name="android.intent.action.VIEW" />  //这里指定ACTION_VIEW 的intent操作
            </intent-filter>
  </activity>

上面的xml代码 为 MainActivity 指定了深层链接,其uri 为 bpp://hxd.app

测试该uri

adb shell am start  -W -a android.intent.action.VIEW
            -d "bpp://hxd.app"   com.example.android

-d 后加 uri 和此应用的包名

但是这种方式是不能在浏览器里直接输入uri 打开对应的apk的。
除了通过adb的方式来测试,还可以通过其他的应用来测试
在另一个app A 里新增一个webview

<WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
   />
   //在对应的activity里 找到该控件 :
		WebView webView = findViewById(R.id.webview);
        webView.loadUrl("file:///android_asset/web/test.html");

其中这个webview加载的是本地的uri,其路径为:
在这里插入图片描述
新增assets目录的方法为: (assert 目录下的系统在编译的时候不会编译assets下的资源文件)新增assert文件夹

在assert是目录想新增web目录 在其中 新建一个test.xml

<html>
<body>
<a href="bpp://hxd.app">open</a>
</body>
</html>

点击A中的open 就可以调到 第一个app的mainactivity了。

除了深层链接,ard在6.0之后引入了应用链接的机制

Android 应用链接是一种特殊类型的深层链接,可让您的网站网址直接在您的 Android 应用中打开相应内容(无需用户选择应用)。
Android 应用链接是一种深层链接,它们基于已验证属于您网站的网站网址。因此,点击某个此类链接会立即打开您的应用(如已安装),并且不会显示消除歧义对话框。不过,用户以后可以更改处理这些链接的偏好设置。

两者的差别
使用应用链接 data scheme 必须为http 或者https 并且要在对应的网站上发布Digital Asset Links 文件
判断一个网址是否发布了此文件 可以通过 下面方式判断

 https://domain.name/.well-known/assetlinks.json  

知乎的Android app的配置如下: 可以参考

<activity
            android:name="com.zhihu.android.app.ui.activity.RouterPortalActivity">

            <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="https"
                    android:host="activity.zhihu.com"
                    android:pathPattern="/.*/.*" />

                <data
                    android:scheme="zhihu"
                    android:host="activity"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="zhihu"
                    android:host="activity"
                    android:pathPattern="/.*/.*" />
            </intent-filter>

            <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="zhihu" />
            </intent-filter>

            <intent-filter
                android:autoVerify="true">

                <action
                    android:name="android.intent.action.VIEW" />

                <category
                    android:name="android.intent.category.DEFAULT" />

                <category
                    android:name="android.intent.category.BROWSABLE" />

                <data
                    android:scheme="http"
                    android:host="ms.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="http"
                    android:host="oia.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/question/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/question/.*/answer/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/answer/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/collection/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/people/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/topic/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/roundtable/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/lives/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/org/.*" />

                <data
                    android:scheme="http"
                    android:host="zhuanlan.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="http"
                    android:host="zhuanlan.zhihu.com"
                    android:pathPattern="/.*/.*" />

                <data
                    android:scheme="http"
                    android:host="promotion.zhihu.com"
                    android:pathPattern="/p/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/publications/.*/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/zhihu" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/collection/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/book/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pin/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/links/.*" />

                <data
                    android:scheme="http"
                    android:host="www.zhihu.com"
                    android:pathPattern="/coupon/.*" />

                <data
                    android:scheme="https"
                    android:host="ms.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="https"
                    android:host="oia.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/question/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/question/.*/answer/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/answer/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/collection/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/people/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/topic/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/roundtable/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/lives/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/org/.*" />

                <data
                    android:scheme="https"
                    android:host="zhuanlan.zhihu.com"
                    android:pathPattern="/.*" />

                <data
                    android:scheme="https"
                    android:host="zhuanlan.zhihu.com"
                    android:pathPattern="/.*/.*" />

                <data
                    android:scheme="https"
                    android:host="promotion.zhihu.com"
                    android:pathPattern="/p/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/publications/.*/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/zhihu" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/collection/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pub/book/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/pin/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/links/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/coupon/.*" />

                <data
                    android:scheme="https"
                    android:host="www.zhihu.com"
                    android:pathPattern="/remix/albums/.*" />
            </intent-filter>
        </activity>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值