Android自定义URI跳转APP

本文介绍了如何在Android中自定义URI以实现从浏览器或其他APP的跳转,并解析传递的参数。首先,定义了自定义URI的协议,然后在HandleFromExternalActivity中解析参数。虽然只设置了scheme,但能完成跳转,只是无法获取到URI内的参数。

1、自定义URI

  • 支持浏览器跳转过来
<activity
    android:name=".activity.basic.SchemeActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/title_activity_scheme"
    android:screenOrientation="portrait">
    <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:host="www.qipaiapp.com"
            android:pathPrefix="/outlink"
            android:scheme="qipai" />
    </intent-filter>
    <!--
     	android:host="Master.hi"
        1、浏览器访问:192.168.2.164/master/indexf.html
        2、点击android超链接(<a href="qipai://www.qipaiapp.com/outlink?type=120&goods=26">android</a>)
        3、在QQ中有效果,部分浏览器支持
    -->
</activity>

2、自定义UIR,参数解析

1、定义协议:

<activity
    android:name=".modules.other.HandleFromExternalActivity"
    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" />
        <!--jrzfve://www.jrzf.com/outlink?platform=jrgjApp&target=publish&params={"shareId": "c4ae43d8"}-->
        <data
            android:host="www.jrzf.com"
            android:path="/outlink"
            android:scheme="jrzfve" />
    </intent-filter>
</activity>

2、HandleFromExternalActivity中解析协议传参:

val action = intent.action
        if (Intent.ACTION_VIEW == action) {
            val uri: Uri? = intent.data
            if (uri != null) {
                // jrzfve://www.jrzf.com/outlink?platform=jrgjApp&target=publish&params={"shareId": "c4ae43d8"}
                val dataStr = intent.dataString
                val queryStr: String? = uri.query
                val target = uri.getQueryParameter("target")
                val platform = uri.getQueryParameter("platform")
                val params = uri.getQueryParameter("params")
                val scheme: String? = uri.scheme
                val host: String? = uri.host
                val path: String? = uri.path
                val encodedPathStr: String? = uri.encodedPath
                Log.w("caowj", "scheme= $scheme")
            } else {
                Log.w("caowj", "uri is null")
            }

3、外部APP跳转方法:

 try {
     val url =  "jrzfve://platform=jrgjApp&target=publish&params={\"shareId\": \"c4ae43d8\"}"
     startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
 } catch (e: ActivityNotFoundException) {
     Log.d("caowj", "未安装对应的APP")
     ToastUtils.showShort("未安装对应的APP")
 } catch (e: Exception) {
     Log.d("caowj", "未知的原因:${e.message}")
     ToastUtils.showShort("未知的原因:${e.message}")
 }

只设置scheme

说明:这种方法能跳转,但是取不到里面的参数。

<activity
    android:name=".modules.other.HandleFromExternalActivity"
    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" />
        <!--jrzfve://platform=jrgjApp&target=publish&params={"shareId": "c4ae43d8"}-->
        <data
            android:scheme="jrzfve" />
    </intent-filter>
</activity>

跳转时使用的URI:

jrzfve://platform=jrgjApp&target=publish&params={"shareId": "c4ae43d8"}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐诺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值