CSDN博客還頭一次用markDown,沒(méi)想到語(yǔ)法不一樣,這邊很多不支持,就這樣了…
https://open.weixin.QQ.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN(官方文檔)
1 . ### 前期準(zhǔn)備工作 * 微信分享sdk準(zhǔn)備好,和微信支付是同一個(gè)sdk * 權(quán)限啊什么的 * APP_ID還有應(yīng)用號(hào)什么的注冊(cè)(最好在 application里面進(jìn)行注冊(cè),這樣 微信支付和微信分享都能直接獲取微信api對(duì)象)
public static IWXAPI wXapi;//微信支付,微信分享 注冊(cè)wXapi = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);wXapi.registerApp(Constants.WX_APPID);2 . ### 與前端之前的交流溝通準(zhǔn)備工作 微信分享分2種情況… * #### 第一種:本地APP內(nèi)進(jìn)行微信分享(無(wú)需前端)
這種情況,需要本地創(chuàng)建popWindow布局,創(chuàng)建popWindow邏輯相關(guān)的類…
微信朋友和微信朋友圈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,需要找美工進(jìn)行處理
通過(guò)自行創(chuàng)建的popWindow的點(diǎn)擊監(jiān)聽(tīng),獲取 點(diǎn)擊的index,獲知 點(diǎn)擊的是 微信朋友還是微信朋友圈.傳遞給 分享邏輯使用…
#### 第二種:本地APP內(nèi)html5頁(yè)面內(nèi)進(jìn)行微信分享(需要與前端進(jìn)行交流溝通)這種情況,需要JS調(diào)用java端代:
在對(duì)應(yīng)的button | div等組件onclick對(duì)應(yīng)的function內(nèi)
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("這是標(biāo)題", "這是描述", url, shareIndex, imgUrl);} else if(isiOS) { shareToWX("這是標(biāo)題", "這是描述", url, shareIndex, imgUrl);}通過(guò)agent嗅探,獲知當(dāng)前OS. window.約定好的名稱.約定好的被調(diào)用方法名(需要的參數(shù)) 進(jìn)行調(diào)用
JS端傳遞的參數(shù)正是微信分享需要的內(nèi)容,因?yàn)槭莌tml5頁(yè)面的內(nèi)容,只能通過(guò)JS傳遞過(guò)來(lái)…
3 . ### api調(diào)用微信分享
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();需要注意的是,根據(jù)分享的內(nèi)容類型,在創(chuàng)建WXMediaMessage時(shí),傳入不同的值…
比如:網(wǎng)頁(yè) webpage,文本 text等…
scence的處理…
根據(jù)本地popWindow的點(diǎn)擊監(jiān)聽(tīng)或者通過(guò)JS端監(jiān)聽(tīng)傳遞過(guò)來(lái)的index進(jìn)行選擇
4 . #### 微信分享結(jié)果回調(diào)處理 微信分享的結(jié)果回調(diào)處理和微信支付類似,必須在com.xxx.wxapi包名內(nèi)創(chuàng)建一個(gè) 名稱固定的 類名
微信支付的是 WXPayEntryActivity
微信分享的是 WXEntryActivity
和微信支付結(jié)果處理頁(yè)面一樣,實(shí)現(xiàn)IWXAPIEventHandler接口…重寫2個(gè)方法.
主要是:
@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(); }最后需要在清單文件中 注冊(cè)該Activity,同時(shí)必須得添加上.. android:exported=”true”
OVER…..
5 . #### 代碼的混淆
-keepclassmembers class 包名$方法名 { public *;}-keepattributes *JavascriptInterface*6 . #### 其他相關(guān)
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) { // 顯示網(wǎng)絡(luò)上的圖片 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; }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注