Android微信SDK實(shí)現(xiàn)分享
用微信提供的SDK來(lái)實(shí)現(xiàn)分享:
從http://open.weixin.qq.com下載Android相關(guān)的jar包,將libammsdk.jar加入到項(xiàng)目中。
微信分享的核心類(lèi),部分代碼如下:
WechatShareManager.java
package com.jackie.umeng.share; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.widget.Toast; import com.tencent.mm.sdk.modelmsg.SendMessageToWX; import com.tencent.mm.sdk.modelmsg.WXImageObject; import com.tencent.mm.sdk.modelmsg.WXMediaMessage; import com.tencent.mm.sdk.modelmsg.WXTextObject; import com.tencent.mm.sdk.modelmsg.WXVideoObject; import com.tencent.mm.sdk.modelmsg.WXWebpageObject; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.WXAPIFactory; /** * 實(shí)現(xiàn)微信分享功能的核心類(lèi) * @author chengcj1 * */ public class WechatShareManager { private static final int THUMB_SIZE = 150; public static final int WECHAT_SHARE_WAY_TEXT = 1; //文字 public static final int WECHAT_SHARE_WAY_PICTURE = 2; //圖片 public static final int WECHAT_SHARE_WAY_WEBPAGE = 3; //鏈接 public static final int WECHAT_SHARE_WAY_VIDEO = 4; //視頻 public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession; //會(huì)話(huà) public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈 private static WechatShareManager mInstance; private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo; private IWXAPI mWXApi; private Context mContext; private WechatShareManager(Context context){ this.mContext = context; //初始化數(shù)據(jù) //初始化微信分享代碼 initWechatShare(context); } /** * 獲取WeixinShareManager實(shí)例 * 非線(xiàn)程安全,請(qǐng)?jiān)赨I線(xiàn)程中操作 * @return */ public static WechatShareManager getInstance(Context context){ if(mInstance == null){ mInstance = new WechatShareManager(context); } return mInstance; } private void initWechatShare(Context context){ if (mWXApi == null) { mWXApi = WXAPIFactory.createWXAPI(context, WechatShareUtil.WECHAT_APP_ID, true); } mWXApi.registerApp(WechatShareUtil.WECHAT_APP_ID); } /** * 通過(guò)微信分享 * @param shareWay 分享的方式(文本、圖片、鏈接) * @param shareType 分享的類(lèi)型(朋友圈,會(huì)話(huà)) */ public void shareByWebchat(ShareContent shareContent, int shareType){ switch (shareContent.getShareWay()) { case WECHAT_SHARE_WAY_TEXT: shareText(shareContent, shareType); break; case WECHAT_SHARE_WAY_PICTURE: sharePicture(shareContent, shareType); break; case WECHAT_SHARE_WAY_WEBPAGE: shareWebPage(shareContent, shareType); break; case WECHAT_SHARE_WAY_VIDEO: shareVideo(shareContent, shareType); break; } } private abstract class ShareContent { protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract int getPictureResource(); } /** * 設(shè)置分享文字的內(nèi)容 * @author chengcj1 * */ public class ShareContentText extends ShareContent { private String content; /** * 構(gòu)造分享文字類(lèi) * @param text 分享的文字內(nèi)容 */ public ShareContentText(String content){ this.content = content; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_TEXT; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } @Override protected int getPictureResource() { return -1; } } /* * 獲取文本分享對(duì)象 */ public ShareContent getShareContentText(String content) { if (mShareContentText == null) { mShareContentText = new ShareContentText(content); } return (ShareContentText) mShareContentText; } /** * 設(shè)置分享圖片的內(nèi)容 * @author chengcj1 * */ public class ShareContentPicture extends ShareContent { private int pictureResource; public ShareContentPicture(int pictureResource){ this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_PICTURE; } @Override protected int getPictureResource() { return pictureResource; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } } /* * 獲取圖片分享對(duì)象 */ public ShareContent getShareContentPicture(int pictureResource) { if (mShareContentPicture == null) { mShareContentPicture = new ShareContentPicture(pictureResource); } return (ShareContentPicture) mShareContentPicture; } /** * 設(shè)置分享鏈接的內(nèi)容 * @author chengcj1 * */ public class ShareContentWebpage extends ShareContent { private String title; private String content; private String url; private int pictureResource; public ShareContentWebpage(String title, String content, String url, int pictureResource){ this.title = title; this.content = content; this.url = url; this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_WEBPAGE; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return title; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return pictureResource; } } /* * 獲取網(wǎng)頁(yè)分享對(duì)象 */ public ShareContent getShareContentWebpag(String title, String content, String url, int pictureResource) { if (mShareContentWebpag == null) { mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureResource); } return (ShareContentWebpage) mShareContentWebpag; } /** * 設(shè)置分享視頻的內(nèi)容 * @author chengcj1 * */ public class ShareContentVideo extends ShareContent { private String url; public ShareContentVideo(String url) { this.url = url; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_VIDEO; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return -1; } } /* * 獲取視頻分享內(nèi)容 */ public ShareContent getShareContentVideo(String url) { if (mShareContentVideo == null) { mShareContentVideo = new ShareContentVideo(url); } return (ShareContentVideo) mShareContentVideo; } /* * 分享文字 */ private void shareText(ShareContent shareContent, int shareType) { String text = shareContent.getContent(); //初始化一個(gè)WXTextObject對(duì)象 WXTextObject textObj = new WXTextObject(); textObj.text = text; //用WXTextObject對(duì)象初始化一個(gè)WXMediaMessage對(duì)象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; //構(gòu)造一個(gè)Req SendMessageToWX.Req req = new SendMessageToWX.Req(); //transaction字段用于唯一標(biāo)識(shí)一個(gè)請(qǐng)求 req.transaction = buildTransaction("textshare"); req.message = msg; //發(fā)送的目標(biāo)場(chǎng)景, 可以選擇發(fā)送到會(huì)話(huà) WXSceneSession 或者朋友圈 WXSceneTimeline。 默認(rèn)發(fā)送到會(huì)話(huà)。 req.scene = shareType; mWXApi.sendReq(req); } /* * 分享圖片 */ private void sharePicture(ShareContent shareContent, int shareType) { Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); WXImageObject imgObj = new WXImageObject(bitmap); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true); bitmap.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); //設(shè)置縮略圖 SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } /* * 分享鏈接 */ private void shareWebPage(ShareContent shareContent, int shareType) { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); if(thumb == null) { Toast.makeText(mContext, "圖片不能為空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } /* * 分享視頻 */ private void shareVideo(ShareContent shareContent, int shareType) { WXVideoObject video = new WXVideoObject(); video.videoUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(video); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.send_music_thumb); // BitmapFactory.decodeStream(new URL(video.videoUrl).openStream()); /** * 測(cè)試過(guò)程中會(huì)出現(xiàn)這種情況,會(huì)有個(gè)別手機(jī)會(huì)出現(xiàn)調(diào)不起微信客戶(hù)端的情況。造成這種情況的原因是微信對(duì)縮略圖的大小、title、description等參數(shù)的大小做了限制,所以有可能是大小超過(guò)了默認(rèn)的范圍。 * 一般情況下縮略圖超出比較常見(jiàn)。Title、description都是文本,一般不會(huì)超過(guò)。 */ Bitmap thumbBitmap = Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true); thumb.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } }
MainActivity.java
package com.jackie.umeng.share; import com.jackie.umeng.share.WechatShareManager.ShareContentPicture; import com.jackie.umeng.share.WechatShareManager.ShareContentText; import com.jackie.umeng.share.WechatShareManager.ShareContentVideo; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button mShareText, mSharePicture, mShareVideo; private WechatShareManager mShareManager; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mShareText = (Button) findViewById(R.id.share_text); mSharePicture = (Button) findViewById(R.id.share_picture); mShareVideo = (Button) findViewById(R.id.share_video); mShareText.setOnClickListener(this); mSharePicture.setOnClickListener(this); mShareVideo.setOnClickListener(this); mContext = this; mShareManager = WechatShareManager.getInstance(mContext); } @Override public void onClick(View v) { if (!isWebchatAvaliable()) { Toast.makeText(mContext, "請(qǐng)先安裝微信", Toast.LENGTH_LONG).show(); return; } switch (v.getId()) { case R.id.share_text: ShareContentText mShareContentText = (ShareContentText) mShareManager.getShareContentText("微信文本分享"); mShareManager.shareByWebchat(mShareContentText, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS); break; case R.id.share_picture: ShareContentPicture mShareContentPicture = (ShareContentPicture) mShareManager.getShareContentPicture(R.drawable.share); mShareManager.shareByWebchat(mShareContentPicture, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS); break; case R.id.share_video: ShareContentVideo mShareContentVideo = (ShareContentVideo) mShareManager.getShareContentVideo("http://baidu.hz.letv.com/kan/agSlT?fr=v.baidu.com/"); mShareManager.shareByWebchat(mShareContentVideo, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS); break; default: break; } } private boolean isWebchatAvaliable() { //檢測(cè)手機(jī)上是否安裝了微信 try { getPackageManager().getPackageInfo("com.tencent.mm", PackageManager.GET_ACTIVITIES); return true; } catch (Exception e) { return false; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)設(shè)計(jì)nowinandroid構(gòu)建腳本學(xué)習(xí)
這篇文章主要為大家介紹了Android開(kāi)發(fā)設(shè)計(jì)nowinandroid構(gòu)建腳本學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android App多個(gè)入口的實(shí)現(xiàn)方法
這篇文章主要介紹了Android App多個(gè)入口的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Android性能優(yōu)化之Bitmap圖片優(yōu)化詳解
在Android項(xiàng)目的imageview中使用大圖bitmap時(shí)會(huì)占據(jù)很大的內(nèi)存,而且在很多時(shí)候我們并不需要顯示原圖那么大的圖片, 所以我們需要對(duì)圖片進(jìn)行優(yōu)化,這篇文章主要介紹了Android性能優(yōu)化之Bitmap圖片優(yōu)化的相關(guān)資料,需要的朋友們下面來(lái)一起看看吧。2017-04-04Android矢量圖之VectorDrawable類(lèi)自由填充色彩
這篇文章主要介紹了Android矢量圖之VectorDrawable類(lèi)自由填充色彩的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05Android 鍵盤(pán)開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)
這篇文章我們給大家總結(jié)了Android 鍵盤(pán)開(kāi)發(fā)的相關(guān)知識(shí)點(diǎn)內(nèi)容以及開(kāi)發(fā)心得,有需要的朋友參考學(xué)習(xí)下。2018-06-06Android 媒體開(kāi)發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析
這篇文章主要介紹了Android 媒體開(kāi)發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析,需要的朋友可以參考下2017-08-08flutter 自定義websocket路由的實(shí)現(xiàn)
這篇文章主要介紹了flutter 自定義websocket路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Android實(shí)現(xiàn)控件拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)控件拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12