首页 > 学院 > 开发设计 > 正文

微信分享全指南

2019-11-09 17:02:13
字体:
来源:转载
供稿:网友

微信分享

CSDN博客还头一次用markDown,没想到语法不一样,这边很多不支持,就这样了…

https://open.weixin.QQ.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN(官方文档)

1 . ### 前期准备工作 * 微信分享sdk准备好,和微信支付是同一个sdk * 权限啊什么的 * APP_ID还有应用号什么的注册(最好在 application里面进行注册,这样 微信支付和微信分享都能直接获取微信api对象)

public static IWXAPI wXapi;//微信支付,微信分享 注册wXapi = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);wXapi.registerApp(Constants.WX_APPID);

2 . ### 与前端之前的交流沟通准备工作 微信分享分2种情况… * #### 第一种:本地APP内进行微信分享(无需前端)

这种情况,需要本地创建popWindow布局,创建popWindow逻辑相关的类…

微信朋友和微信朋友圈icon资源:

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319171&token=&lang=zh_CN 需要注意的是,微信朋友圈的图片大小是600 x 600,需要找美工进行处理

通过自行创建的popWindow的点击监听,获取 点击的index,获知 点击的是 微信朋友还是微信朋友圈.传递给 分享逻辑使用…

#### 第二种:本地APP内html5页面内进行微信分享(需要与前端进行交流沟通)

这种情况,需要JS调用java端代:

JAVA端

/***"Android"可自行修改,并与前端一致*API17以上时,需要添加@JavascriptInterface注解*webView设置settings.setJavascriptEnabled(true);*/webView.addJavascriptInterface(new JSInterface (),"Android");class JSInterface { @JavascriptInterface public void shareToWX(String title, String description, String url, String shareIndex, String imgurl){ //这里是 分享的具体逻辑,由JS调用 }}

JS端

在对应的button | div等组件onclick对应的function内

var u = navigator.userAgent;var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端var isiOS = !!u.match(//(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端if(isAndroid) { window.Android.shareToWX("这是标题", "这是描述", url, shareIndex, imgUrl);} else if(isiOS) { shareToWX("这是标题", "这是描述", url, shareIndex, imgUrl);}

通过agent嗅探,获知当前OS. window.约定好的名称.约定好的被调用方法名(需要的参数) 进行调用

JS端传递的参数正是微信分享需要的内容,因为是html5页面的内容,只能通过JS传递过来…


3 . ### api调用微信分享

WXWebpageObject webpage = new WXWebpageObject();webpage.webpageUrl = url;WXMediaMessage msg = new WXMediaMessage(webpage);msg.title = title;msg.description = description;Bitmap bmp = BitmapUtils.getbitmap(imgurl);Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);bmp.recycle();msg.thumbData = BitmapUtils.bmpToByteArray(thumbBmp, true);SendMessageToWX.Req req = new SendMessageToWX.Req();req.transaction = buildTransaction("webpage");req.message = msg;//0是分享到 微信朋友,1是分享到 微信朋友圈if (Integer.parseInt(shareIndex) == 0) { mTargetScene = SendMessageToWX.Req.WXScenesession;} else if (Integer.parseInt(shareIndex) == 1) { mTargetScene = SendMessageToWX.Req.WXSceneTimeline;}req.scene = mTargetScene;MyBaseApplication.wXapi.sendReq(req);finish();

需要注意的是,根据分享的内容类型,在创建WXMediaMessage时,传入不同的值…

比如:网页 webpage,文本 text等…

scence的处理…

根据本地popWindow的点击监听或者通过JS端监听传递过来的index进行选择


4 . #### 微信分享结果回调处理 微信分享的结果回调处理和微信支付类似,必须在com.xxx.wxapi包名内创建一个 名称固定的 类名

微信支付的是 WXPayEntryActivity

微信分享的是 WXEntryActivity

和微信支付结果处理页面一样,实现IWXAPIEventHandler接口…重写2个方法.

主要是:

@Override public void onResp(BaseResp baseResp) { int result = 0; switch (baseResp.errCode) { case BaseResp.ErrCode.ERR_OK: result = R.string.wx_share_notice_success; break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = R.string.wx_share_notice_cancel; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = R.string.wx_share_notice_deny; break; default: result = R.string.wx_share_notice_unkown; break; } ToastUtils.show(this, result); finish(); }

最后需要在清单文件中 注册该Activity,同时必须得添加上.. android:exported=”true”

OVER…..


5 . #### 代码的混淆

-keepclassmembers class 包名$方法名 { public *;}-keepattributes *JavascriptInterface*

6 . #### 其他相关

PRivate String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); }public class BitmapUtils { public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public static Bitmap getbitmap(String imageUri) { // 显示网络上的图片 Bitmap bitmap = null; try { URL myFileUrl = new URL(imageUri); HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (OutOfMemoryError e) { e.printStackTrace(); bitmap = null; } catch (IOException e) { e.printStackTrace(); bitmap = null; } return bitmap; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表