本文是用的URL Scheme实现的

h5页面代码:

URI主要分三个部分:scheme、host、pathPrefix。

scheme="share"

host="jianshu.com"

pathPrefix="/news/content"

组成的链接share://jianshu.com/news/content

用?key=value&key=value方式进行传递数据

即sourceSid=${article.sid} 则是携带过来的数据

Android

可通过Activity的onCreate()方法来获取携带的值

// app跳转与h5跳转,取sourceSid

String sourceSid = getIntent().getStringExtra(sourceSid);

if (TextUtils.isEmpty(sourceSid)) {

// 通过h5网页调起app

Intent mgetvalue = getIntent();

String maction = mgetvalue.getAction();

if (Intent.ACTION_VIEW.equals(maction)) {

Uri uri = mgetvalue.getData();

if (uri != null) {

// 通过URL获取value

sourceSid = uri.getQueryParameter("sourceSid");

}

}

} else {

// app内部跳转过来

}

AndroidManifest设置滤器

android:name=".NewsActivity"

android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"

android:launchMode="singleTask"

android:theme="@style/AppTheme.ShareStyle">

android:host="jianshu.com"

android:pathPrefix="/news/content"

android:scheme="share" />

当App未启动时,点击H5跳转App时会出现白色页面(是否是白色和设置的主题有关,Application-onCreate()...)然后再进入详情页面,会给人一种延迟卡顿感。

设置theme 在延迟的这段时间内展示欢迎图片,可以设置为单独的Activity也可以设置为全局

@mipmap/welcome

后记:

一般都是用App分享链接到微信、QQ的。

而微信、QQ的浏览器屏蔽了iframe,所以不能点击分享的链接直接打开App了。

H5可通过给button设置链接方式:

< a href="share://jianshu.com/news/content?sourceSid=${article.sid}">App内打开 a>

然而微信屏蔽了URL Scheme,通过判断是微信浏览器则显示给用户提示:让其用浏览器的方式进行打开,然后再通过在浏览器跳转App。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐