1.android端和web端协议好拦截规则
这里假设分享渠道是微信,规则是url末尾带上”sharechannel=weixin”
2.在WebViewClient类的shouldOverriderUrlLoading方法中拦截,在网页加载完时调用读取Html的document中相应标签的Javascript代码(分享内容定义在相应的标签中)
/**
* 读取网页分享内容,成功后再回调方法onSuccess中发送分享
*
* @param type 分享平台
* @param functionType 功能类型 1:分享 2:复制链接
*/
public boolean readShareFromHtml(int type, int functionType) {
//在网页加载完时调用
if (!isLoaded) {
ToastUtil.getInstance(mActivity).showShort("操作太快咯,请稍后再试");
}else {
if (currentWebView != null) {
currentWebView.loadUrl("javascript:try{window.DOCUMENT.setShare(document.getElementById(\"sharecontent\").content || '',document.getElementById(\"shareimg\").src || '',document.getElementById(\"shareurl\").href || '',document.getElementById(\"sharetitle\").content || ''," + type + "," + function