通过浏览器链接打开本地应用(APP)

编程知识 更新时间:2023-05-02 19:47:13

前言:业务场景,一个分享出去的h5界面通过页面内某个事件的触发,启动目标app并执行相关逻辑处理或做其他页面跳转(如:跳应用市场下载应用等)。下面是我在企业开发过程中,实操的记录,对于有这块需求的朋友,可以来参考下。

Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
前端页面:(界面中触发事件的入口)

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>

Android端:
1.在AndroidManifest.xml的MAIN Activity下追加以下内容(按照下面的格式来追加)

<intent-filter>  
    <action android:name="android.intent.action.MAIN"/>  
    <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>  
<!-- 在MAIN的同级处加入过滤器,不然会导致应用图标在桌面消失等问题 -->
<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="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

2.如果需要取值,在代码中进行如下操作:(测试可行)

Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}

补充:
各个项目含义如下所示:
scheme:判别启动的App。 ※详细后述
host:适当记述
path:传值时必须的key ※没有也可以
query:获取值的Key和Value ※没有也可以

更多推荐

通过浏览器链接打开本地应用(APP)

本文发布于:2023-04-28 09:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/c6e9e97a57c518a8fd8a3b23ee832043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   链接   APP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 108266文章数
  • 27373阅读数
  • 0评论数