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¶ms={"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¶ms={"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¶ms={\"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¶ms={"shareId": "c4ae43d8"}-->
<data
android:scheme="jrzfve" />
</intent-filter>
</activity>
跳转时使用的URI:
jrzfve://platform=jrgjApp&target=publish¶ms={"shareId": "c4ae43d8"}

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

被折叠的 条评论
为什么被折叠?



