開發(fā)中分享操作往往經(jīng)常遇到收夸,而且還是一些比較大型一定的平臺,如微信卧惜,QQ,微博等咽瓷。寫這篇博客主要是把微信的的分享和相關(guān)操作表達(dá)一下,分享可以包含:文字忱详,視頻围来,音樂匈睁,圖片等分享监透。
分享可以有 分享給好友,群航唆,朋友圈等胀蛮。
效果如下圖:(注意:功能根據(jù)自己的需要選擇就可以了)
準(zhǔn)備工作:
微信開放平臺創(chuàng)建應(yīng)用,操作步驟請看博客:Android之微信開放平臺創(chuàng)建應(yīng)用
成功之后得到AppId糯钙,放入項(xiàng)目中粪狼。
public class Constants {
// APP_ID 替換為你的應(yīng)用從官方網(wǎng)站申請到的合法appId==
public static final String APP_ID = "wx1b73fd7b49ffa027";
public static class ShowMsgActivity {
public static final String STitle = "showmsg_title";
public static final String SMessage = "showmsg_message";
public static final String BAThumbData = "showmsg_thumb_data";
}
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI api = WXAPIFactory.createWXAPI(context, null);
// 將該app注冊到微信
api.registerApp(Constants.APP_ID);
}
}
文本分享代碼:
// 初始化一個(gè)WXTextObject對象
WXTextObject textObj = new WXTextObject();
textObj.text = text;
// 用WXTextObject對象初始化一個(gè)WXMediaMessage對象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
// 發(fā)送文本類型的消息時(shí),title字段不起作用
msg.title = "Will be ignored";
msg.description = text;
// 構(gòu)造一個(gè)Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text"); // transaction字段用于唯一標(biāo)識一個(gè)請求
req.message = msg;
/**
* 判斷是否是朋友圈
*/
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
// 調(diào)用api接口發(fā)送數(shù)據(jù)到微信
api.sendReq(req);
圖片分享代碼:(包含 本地圖片任岸,SD卡圖片再榄,網(wǎng)絡(luò)圖片)
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true); // 設(shè)置所圖;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
音樂分享代碼
WXMusicObject music = new WXMusicObject();
music.musicUrl="http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = music;
msg.title = "標(biāo)題";
msg.description = "描述信息";
Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
msg.thumbData = Util.bmpToByteArray(thumb, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("music");
req.message = msg;
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
視頻分享代碼
WXVideoObject video = new WXVideoObject();
video.videoUrl = "url....mp4";//mp4視頻路徑
WXMediaMessage msg = new WXMediaMessage(video);
msg.title = "標(biāo)題";
msg.description = "描述信息";
Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb); msg.thumbData = Util.bmpToByteArray(thumb, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; api.sendReq(req);
網(wǎng)頁分享代碼:
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "http://www.baidu.com";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "標(biāo)題";
msg.description = "描述信息";
Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
msg.thumbData = Util.bmpToByteArray(thumb, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
等等享潜。
代碼下載
github: https://github.com/DickyQie/android-third-party/tree/weixin-share