国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

微信分享全指南

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; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清涧县| 香格里拉县| 石嘴山市| 绥中县| 县级市| 南宁市| 陆川县| 新晃| 五原县| 建湖县| 庐江县| 新郑市| 上饶市| 巴彦县| 金阳县| 姜堰市| 临汾市| 曲麻莱县| 田阳县| 铜梁县| 晋江市| 平果县| 天全县| 宕昌县| 罗城| 平塘县| 兰州市| 宜州市| 肃南| 邓州市| 晋中市| 宿州市| 马边| 大足县| 湖南省| 临漳县| 五莲县| 西和县| 英吉沙县| 高清| 英吉沙县|