欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Android之scheme使用

发布时间:2023/12/20 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Android之scheme使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1.准备Html文件 - 将其拷贝进SDCard

<!DOCTYPE html> <html><head><meta charset="UTF-8"><meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,target-densitydpi = medium-dpi,viewport-fit=cover"><meta name="format-detection" content="telephone=no"><meta name="apple-touch-fullscreen" content="YES"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta http-equiv="X-UA-Compatible" content="ie=edge"> </head><body><!-- <a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> --><!-- scheme:判别启动的App --><!-- host:主机 没有也可以 --><!-- path:传值时必须的key 没有也可以 --><!-- query:获取值的Key和Value 没有也可以 --><a href="zkweb://zk/webjump?name=zhangsan&age=27">启动应用</a><br/></body></html>

2.AndroidManifest.xml配置

<activity android:name="com.xxx.MainActivity"android:exported="true"android:launchMode="singleTask"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><!-- scheme设置 --><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="zkweb"/></intent-filter> </activity>

注意: scheme名称只能由小写字母构成,最好不要有数字及其他符号,笔者这里原来写的zk_web调试了半天不能跳转。嘤嘤嘤~

3.打开SDCard中Html文件 - 启动应用 

//scheme Intent intent = getIntent(); String action = intent.getAction(); if (Intent.ACTION_VIEW.equals(action)) {Uri uri = intent.getData();if (uri != null) {String name = uri.getQueryParameter("name");String age = uri.getQueryParameter("age");Log.e(getClass().getSimpleName(), "name=" + name + " age=" + age);} }

日志输出:2022-02-22 16:43:12.540 24569-24569/com.xxx E/MainActivity: name=zhangsan age=27

4.实用说明

A. 一般实际开发中配置 android:scheme名称 + 自定义数据格式数据,比如:

<a href="scheme名称://URLEncoder.encode(json数据)">测试跳转</a>

B.实际开发中用scheme传参数避免不了中文乱码问题,一般直接跟前端开发约定,拼接好所有待传数据,使用URLEncoder.encode()处理整体数据,客户端接收到scheme数据后再通过URLDecoder.decode()处理,从得到原始数据

总结

以上是生活随笔为你收集整理的Android之scheme使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。