概述
我们的项目通常会有一个m版,即从手机浏览器查看网站看到的,我们更希望用户从m版跳到我们的app观看操作。这时候,我们会在右下角添加一个,跳转app的按钮。
实现
功能的实现,要求负责h5的同学在m版的h5界面添加一个a标签跳转用。
类似于这种格式即可。
<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>
比如:<a href="myapp://360.app/main?id=1">启动应用程序</a>
scheme:判别启动的App,不可跟其他app重复。
host: 适当记述
path: 用来区分哪个界面。
query:传值
app中配置:
首先在AndroidManifest.xml的自己需要跳转的activity下追加以下内容。
<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="myapp" android:host="360.app" android:pathPrefix="/main"/>
</intent-filter>
host之前不需要加:// ,pathPrefix的/ 不能省略。
intent-filter 启动的,和这个不能混淆,还要追加。
传值处理
如果我们需要跳转时候传值(跳到app内页需要携带参数,如果是跳到首页则不需要)
接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:
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");
}
}
这样就能获取到URL传递过来的值了。
本文详细介绍了一种从H5页面跳转至APP的具体实现方案,包括如何在H5中设置跳转链接,以及在Android应用中配置接收跳转的Intent。通过在AndroidManifest.xml中添加特定的intent-filter,可以实现从外部链接直接打开APP指定页面并传递参数。
1573

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



