做一个活动,在app里嵌入一个H5页面。

如何让用户点击H5页面上的按钮跳转到APP的另一个页面?

比如说点击H5页面上的充值按钮,跳转到充值界面,点击立即抢购按钮,跳转到商品页面。

H5与APP端是如何进行数据交互的?

后台该如何写?

有没有例子?

请大牛之支招。

回复内容:

做一个活动,在app里嵌入一个H5页面。

如何让用户点击H5页面上的按钮跳转到APP的另一个页面?

比如说点击H5页面上的充值按钮,跳转到充值界面,点击立即抢购按钮,跳转到商品页面。

H5与APP端是如何进行数据交互的?

后台该如何写?

有没有例子?

请大牛之支招。

上面几个方法都挺不错的。但其实只是单纯跳转问题,不用搞得那么复杂。

从webview中抓取跳转信息,然后android端和前段商量好接口,就直接处理就好了。

例如:web中有个跳转

点击后会出发webview中的shouldOverrideUrlLoading函数,Android端:

webView.setWebViewClient(new WebViewClient() {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

L.i(url); // 获取到 example://jumpToSettings ,然后接下来就是字符串处理了

optionUrl(url); // 判断如果是跳转字符串,进行跳转

return true; // 消费,不让网页跳转

}

@Override

public void onPageStarted(WebView view, String url, Bitmap favicon) {

// super.onPageStarted(view, url, favicon);

}

});

JavaScriptCore网上很多例子的,apple文档也很详细

Android端,WebView加载H5,H5中js代码可以这样:

在当前WebView所在Activity这样写:

mWebViewContent.addJavascriptInterface(new JumpAppInterFace(mContext),"toActivity");

其中,JumpAppInterFace 就是你需要注入的跳转到另一Activity的类,大致长这样:

public class JumpAppInterFace {

private static final String TAG = "JumpAppInterFace";

private android.content.Context Context;

public JumpAppInterFace(android.content.Context Context) {

this.Context = Context;

}

@JavascriptInterface

public void OpenLinkH5(String url){

if (!TextUtil.isEmpty(url)){

Intent intent=new Intent(Context, AnotherActivity.class);

intent.putExtra("url",url);

Context.startActivity(intent);

}

}

}

stackoverflow上看到的,你看行不行.

webview.addJavascriptInterface(new Object() {

@JavascriptInterface

public void openActivity(String activity){

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(activity));

mContext.startActivity(i);

}

}, "android");

充值

抢购

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐