所以我为什么不知道深度链接(deep link)

听这个词,以为是很深奥的东西啊

  • 深度链接是一种通过URL来跳转到应用内部页面的方式,适用于跨应用或应用外的页面跳转,也可用于模块化架构下的页面导航。

从事Android开发十多年,这东西见多了,但是真的没有自己去跳转过。

如果是在Android中跳转

 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("example://wang.com/page/system"))
                startActivity(intent)

如果在网页中

需要进行服务配置,这里略过

然后再Android中添加intent filter

<activity android:name=".activity.SystemActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- Accepts URIs that begin with "example.com" -->
<!--                <data android:scheme="http" android:host="example.com" />-->
                <!-- Accepts URIs that begin with "example" -->
                <data android:scheme="example" android:host="wang.com"
                    android:path="/page/system"/>
            </intent-filter>
        </activity>

因为我们平时习惯了用路由框架,第一反应就是用框架,这个还要自己管理intent filter这样。

Pl 的有个视频也讲到这个

https://www.youtube.com/watch?v=5p90O3TPGkY&t=1151s

通过navigation来实现深度链接,autoverify和api等级有关,先声明activity具有处理某个格式data的能力,也可以是通过浏览器来打开的

 <intent-filter android:autoVerify="true">
                <data android:scheme="https" />
                <data android:host="pl-coding.com" />
                <data android:host="www.pl-coding.com" />

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

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

在activity中导航host,可直接从deeplink中抽取出 id,这个也太方便了

val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = HomeScreen,
                        modifier = Modifier
                            .padding(innerPadding)
                    ) {
                        composable<HomeScreen> {
                            HomeScreen()
                        }
                        composable<DeepLinkScreen>(
                            //nav 的类型转换
                            deepLinks = listOf(
                                navDeepLink<DeepLinkScreen>(
                                    basePath = "https://$DEEPLINK_DOMAIN"
                                )
                            )
                        ) {
                            ////nav 的类型转换
                            val id = it.toRoute<DeepLinkScreen>().id
                            Box(
                                modifier = Modifier.fillMaxSize(),
                                contentAlignment = Alignment.Center
                            ) {
                                Text(text = "The ID is $id")
                            }
                        }
                    }
                

那这个intent要怎么发呢

val activityIntent = Intent(this, MainActivity::class.java).apply {
            data = "https://$DEEPLINK_DOMAIN/87".toUri()
        }
     

但是从api 33开始,就是为了验证应用拥有对应的主机域名地址,所以需要上传应用信息到服务器

https://developer.android.com/training/app-links/verify-android-applinks?hl=zh-cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值