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端代:
在對應的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; }}新聞熱點
疑難解答