Android之微信开放平台实现分享(分享好友和朋友圈)

开发中分享操作往往经常遇到,而且还是一些比较大型一定的平台,如微信,QQ,微博等。写这篇博客主要是把微信的的分享和相关操作表达一下,分享可以包含:文字,视频,音乐,图片等分享。

分享可以有 分享给好友,群,朋友圈等。

效果如下图:(注意:功能根据自己的需要选择就可以了)

Android之微信开放平台实现分享(分享好友和朋友圈)

准备工作:

微信开放平台创建应用,操作步骤请看博客:Android之微信开放平台创建应用

成功之后得到AppId,放入项目中。

public class Constants {
// APP_ID 替换为你的应用从官方网站申请到的合法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);
}
}

文本分享代码:

// 初始化一个WXTextObject对象
WXTextObject textObj = new WXTextObject();
textObj.text = text;
// 用WXTextObject对象初始化一个WXMediaMessage对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
// 发送文本类型的消息时,title字段不起作用
msg.title = "Will be ignored";
msg.description = text; // 构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求 req.message = msg;
/**
* 判断是否是朋友圈
*/
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; // 调用api接口发送数据到微信
api.sendReq(req);

图片分享代码:(包含 本地图片,SD卡图片,网络图片)

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); // 设置所图; 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 = "标题";
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 = "标题";
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);

网页分享代码:

WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "http://www.baidu.com";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "标题";
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

上一篇:ios开发-日期处理(类似朋友圈,微博等的发送时间)


下一篇:linux下使用split 来分割大文件