码迷,mamicode.com
首页 > 微信 > 详细

Android 使用微信开放平台分享内容到微信

时间:2015-08-20 10:38:30      阅读:798      评论:0      收藏:0      [点我收藏+]

标签:android   微信   开放平台   分享图片   朋友圈   

应用审核通过后会分配AppId和AppSecret,用以标识我们的应用:
技术分享

接口功能说明:
技术分享

“发送到朋友圈”和“发送给朋友”是我们比较常用的功能,默认已经开启,可以直接使用。

首先将libammsdk.jar放到工程lib目录下。

注册微信接口:

private IWXAPI weApi;
private boolean isRegSuccess = false;

// (Context context, String appId)
weApi = WXAPIFactory.createWXAPI(this, null);
isRegSuccess = weApi.registerApp(Constant.WeChat.APP_ID);
Log.v(Constant.TAG, "isRegSuccess:" + isRegSuccess);

发送文字到微信:

    WXTextObject textObj = new WXTextObject();
                                        textObj.text = text;
                                        WXMediaMessage msg = new WXMediaMessage();
                                        msg.mediaObject = textObj;
                                        msg.description = text;

                                        SendMessageToWX.Req req = new SendMessageToWX.Req();
                                        req.transaction = buildTransaction("text"); 
                                        req.message = msg;
                                        req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
                                                : SendMessageToWX.Req.WXSceneSession;

                                        api.sendReq(req);

发送图片到微信

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 = WeChatUtil
                                                    .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://www.baidu.com";
                                            music.musicUrl = "http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3";
                                            // music.musicUrl="http://120.196.211.49/XlFNM14sois/AKVPrOJ9CBnIN556OrWEuGhZvlDF02p5zIXwrZqLUTti4o6MOJ4g7C6FPXmtlh6vPtgbKQ==/31353278.mp3";

                                            WXMediaMessage msg = new WXMediaMessage();
                                            msg.mediaObject = music;
                                            msg.title = "title ";
                                            msg.description = "description ";

                                            Bitmap thumb = BitmapFactory
                                                    .decodeResource(
                                                            getResources(),
                                                            R.drawable.send_music_thumb);
                                            msg.thumbData = WeChatUtil
                                                    .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 = "http://www.baidu.com";

                                            WXMediaMessage msg = new WXMediaMessage(
                                                    video);
                                            msg.title = "Video Title";
                                            msg.description = "Video Description";
                                            Bitmap thumb = BitmapFactory
                                                    .decodeResource(
                                                            getResources(),
                                                            R.drawable.send_music_thumb);
                                            msg.thumbData = WeChatUtil
                                                    .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 = "WebPage Title";
                                            msg.description = "WebPage Description";
                                            Bitmap thumb = BitmapFactory
                                                    .decodeResource(
                                                            getResources(),
                                                            R.drawable.send_music_thumb);
                                            msg.thumbData = WeChatUtil
                                                    .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);

发送表情

ase MMAlertSelect1: {
                                            WXEmojiObject emoji = new WXEmojiObject();
                                            emoji.emojiPath = EMOJI_FILE_PATH;

                                            WXMediaMessage msg = new WXMediaMessage(
                                                    emoji);
                                            msg.title = "Emoji Title";
                                            msg.description = "Emoji Description";
                                            msg.thumbData = WeChatUtil
                                                    .readFromFile(
                                                            EMOJI_FILE_THUMB_PATH,
                                                            0,
                                                            (int) new File(
                                                                    EMOJI_FILE_THUMB_PATH)
                                                                    .length());

                                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                                            req.transaction = buildTransaction("emoji");
                                            req.message = msg;
                                            req.scene = isTimelineCb
                                                    .isChecked() ? SendMessageToWX.Req.WXSceneTimeline
                                                    : SendMessageToWX.Req.WXSceneSession;
                                            api.sendReq(req);

版权声明:本文原创,转载请注明出处:http://blog.csdn.net/zhoumushui

Android 使用微信开放平台分享内容到微信

标签:android   微信   开放平台   分享图片   朋友圈   

原文地址:http://blog.csdn.net/zhoumushui/article/details/47802883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!