前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从程序外部(浏览器)吊起app

从程序外部(浏览器)吊起app

作者头像
程思扬
发布2022-01-10 14:55:21
5160
发布2022-01-10 14:55:21
举报
文章被收录于专栏:程思阳的专栏

有些项目可能要求从程序外部吊起自己的app,实现做法十分简单。

就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下:

scheme://host:port/path

点击浏览器中的URL链接,启动特定的App。

首先做成HTML的页面,页面内容格式如下:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>

这一句就可以了。

各个项目含义如下所示:

scheme:判别启动的App。 ※详细后述

host:适当记述

path:传值时必须的key ※没有也可以

query:获取值的Key和Value ※没有也可以

代码语言:javascript
复制
<ahref="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>

接下来是Android端。

首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动Activity时给予)

※必须添加项

代码语言:javascript
复制
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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"/>

HTML记述的内容加入<data …/>。

其中必须的内容仅scheme,没有其他内容app也能启动。

※注意事项:intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合。

所以,如果加入了同一个Activity,请按以下这样做,否则会导致应用图标在桌面消失等问题。

代码语言:javascript
复制
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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>

这样的话,没有问题。

接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:

代码语言:javascript
复制
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");
}


这样就能获取到URL传递过来的值了。
经过我测试,在手机自带的浏览器打开上面的html之后,点击启动应用程序,可以顺利地吊起app。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/08/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档