Linkt 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
Linkt 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
Linkt 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 MIT
开发语言 Kotlin 查看源码 »
操作系统 Android
软件类型 开源软件
开源组织
地区 不详
投 递 者 Alias_Travis
适用人群 未知
收录时间 2021-09-28

软件简介

Linkt 是一个轻量级和简单的 Kotlin 库,用于 Android 上的 DeepLink 处理。

设置

配置根目录build.gradle

allprojects {  
    repositories {  
        ...  
        maven { url 'https://jitpack.io' }  
    }  
}  

Linkt添加到项目build.gradle

dependencies {
  implementation 'com.github.jeziellago:Linkt:TAG'
}
  1. 创建DeepLinkModule并注册 deeplinks
class MyDeepLinkModule : DeepLinkModule {

    override fun load() {
        deepLinkOf(
            "linkt://sample",
            "linkt://sample/{userId}/{userName}"
        ) { context, bundle ->
            Intent(context, MainActivity::class.java)
                .apply { putExtras(bundle) }
        }
    }
}

在多模块项目中,你应该有一个或多个 DeepLinkModule。

  1. 将你的模块注册到Application#onCreate, 中DeepLinkLoader#setup
class  MyApplication : Application () {
     override  fun  onCreate () {
         super .onCreate()
         DeepLinkLoader .setup( MyDeepLinkModule ()) 
    } 
}
  1. 创建DeepLinkActivity,并调用DeepLinkLoader#loadFrom
class DeepLinkActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // resolve deeplink
        DeepLinkLoader.loadFrom(this)
    }
}

不要忘记配置AndroidManifest.xml

<activity
    android:name="org.linkt.DeepLinkActivity"
    android:theme="@android:style/Theme.NoDisplay">
    <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="mycompany" />
    </intent-filter>
</activity>

如何从 deeplink 获取数据

路径参数

  • Template linkt://sample/{userId}/{userName}
  • Received: linkt://sample/9999/Jose
// get path parameters
val userId = intent.extras.getString("userId")
val userId = intent.extras.getString("userName")

查询参数

  • Template: linkt://sample
  • Received linkt://sample?subject=Linkt&name=Sample
// get query parameters
val subject = intent.extras.getString("subject")
val name = intent.extras.getString("name")

路径+查询参数

  • Template linkt://sample/{userId}/{userName}
  • Received linkt://sample/999/Jose?subject=Linkt&name=Sample
// get path parameters
val userId = intent.extras.getString("userId")
val userId = intent.extras.getString("userName")
// get query parameters
val subject = intent.extras.getString("subject")
val name = intent.extras.getString("name")

 

展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
0 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部