赋料扬雄敌,诗看子建亲。这篇文章主要讲述android 集成QQ互联 (登录,分享)相关的知识,希望能为你提供帮助。
参考:http://blog.csdn.net/syz8742874/article/details/39271117
http://blog.csdn.net/woblog/article/details/51095087
Android微信SDK实现分享
里面有写好的manager可以直接使用 ,感谢博主分享 ,但里面有个要注意的地方就是博主为了分享内容的复用导致如果你想分享不同内容的时候出现分享的还是上一个的分享,内容具体请看下面的我写的微信分享的代码红字注释
Android微信登录以及分享集成步骤
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&
t=resource/res_list&
verify=1&
id=1417751808&
token=430d21e953346a7a3bffe76d7328630a8ea2466a&
lang=zh_CN
微信官方文档
常见问题 :
1.QQ空间分享打不开不报错,但就是打不开页面
注意:有可能你写成了是这段代码
private void shareToQzone () { //分享类型 params.putString(QzoneShare.SHARE_TO_QQ_KEY_TYPE,SHARE_TO_QZONE_TYPE_IMAGE_TEXT ); params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题"); //必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摘要"); //选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "跳转URL"); //必填 params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, "图片链接ArrayList"); mTencent.shareToQzone(activity, params, new BaseUiListener()); }
你注意这里的params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, "图片链接ArrayList");
这里是错误的,因为要写成这样
ArrayList< String> imageUrls = new ArrayList< String> ();
imageUrls.add("https://pic.cnblogs.com/avatar/677455/20150707171133.png");
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls);
后面是个集合不是一个String
应该是这样写代码
/** * 分享到QQ空间 */ public static void shareToQzone(Context context,IUiListener listener) { //分享类型 final Bundle params = new Bundle(); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题"); //必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摘要"); //选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://www.qq.com/news/1.html"); //必填 ArrayList< String> imageUrls = new ArrayList< String> (); imageUrls.add("https://pic.cnblogs.com/avatar/677455/20150707171133.png"); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls); mTencent.shareToQzone((Activity)context, params, listener); }
传的参是 list 而且params.putStringArrayList() 而不是params.puString();
QQ分享本地和网络图片跟下面的红色标注有关 QQShare开头
/** * QQ分享内容 * 分享的是本地图片 SHARE_TO_QQ_IMAGE_LOCAL_URL * 分享的是网络图片 SHARE_TO_QQ_IMAGE_URL * @param context * @param listener */ public static void shareText(Context context, BeanShareInfo info,IUiListener listener) { final Bundle params = new Bundle(); params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT); params.putString(QQShare.SHARE_TO_QQ_TITLE, info.getTitle()); params.putString(QQShare.SHARE_TO_QQ_SUMMARY, info.getDescription()); params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, info.getTargetUrl()); params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, info.getShareCoverUrl()); params.putString(QQShare.SHARE_TO_QQ_APP_NAME,info.getAppName()); params.getInt(QQShare.SHARE_TO_QQ_EXT_INT); mTencent.shareToQQ((Activity)context, params, listener); }
QQ空间分享也是 但注意的是QQ空间是 QzoneShare 开头
/** * 分享到QQ空间 * 分享的是本地图片 SHARE_TO_QQ_IMAGE_LOCAL_URL * 分享的是网络图片 SHARE_TO_QQ_IMAGE_URL */ public static void shareToQzone(Context context, BeanShareInfo info, IUiListener listener) { //分享类型 final Bundle params = new Bundle(); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TITLE,info.getTitle()); //必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, info.getDescription()); //选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, info.getTargetUrl()); //必填 ArrayList< String> imageUrls = new ArrayList< String> (); imageUrls.add(info.getShareCoverUrl()); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls); mTencent.shareToQzone((Activity)context, params, listener); }
微信常见问题:
android微信分享中bmpToByteArray报错的解决方案
【android 集成QQ互联 (登录,分享)】 1.微信分享网络图片 多看看官方demo里面有详细的用法 ,这里加线程是为了不在主线程中加载网络图片。(不然会报异常)
/* * 分享链接网络图片链接 */ private void shareWebPage(final ShareContent shareContent, final int shareType) {new Thread(new Runnable() { @Override public void run() { try { 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()); Bitmap bmp = BitmapFactory.decodeStream(new URL(shareContent.getPictureUrl()).openStream()); Bitmap thumb = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); if (thumb == null) { Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = https://www.songbingjia.com/android/Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }catch (Exception e){ e.printStackTrace(); } } }).start(); }
2.集成微信登录和微信分享的返回码的地方区分。
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp resp) { String result = ""; if (resp != null) { resp = resp; } switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK:
// 这里可以使用 官方API调用 resp.getType() 获取是登录后返回还是分享后返回 登录返回为1 分享返回为2 if(resp.getType() == WE_LOGIN) { result = "登录成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); String code = ((SendAuth.Resp) resp).code; mIWxData.getCode(code); }else if(resp.getType() == WE_SHARE){ result = "分享成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); LoadDialog.dismiss(this); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "登录被拒绝"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "取消登录"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } }
3.分享时出现hit push hold
解决办法是:原因是你分享的微信账号在其他手机登陆了,但是本机还没有退出登陆,导致没有权限分享。重新进入微信并登陆就好了。
示例集成微信:
注意:关于放入SDK库文件我就不说了,下面是放入后的代码说明
1.在android 的Application 中注册微信 (注意我只是展示了application中关于微信的代码 ,不代表里面就这些代码,当然还有其他的代码)
/** * 微信API */ public static IWXAPI api; /** * 初始化微信 */ private void registWX() { //通过WXAPIFactory工厂 api = WXAPIFactory.createWXAPI(this,WX_APP_ID,false); api.registerApp(WX_APP_ID); }
2.先集成微信分享 可以编写微信管理类,是参考的上面博客中的内容,直接拿来用然后自己按自己需求改了改的
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; 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; import java.net.URL; /** * * 微信分享管理类 * */ 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; //会话 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; //初始化数据 //初始化微信分享代码 initWechatShare(context); }/** * 获取WeixinShareManager实例 * 非线程安全,请在UI线程中操作 * @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, McvApplication.WX_APP_ID, true); mWXApi = McvApplication.api; //如果你在Appcation中注册了微信这里可用Appcation的api 我这里使用了 } //mWXApi.registerApp(McvApplication.WX_APP_ID); //appcation中注册过了 }/** * 通过微信分享 * @param shareContent 分享的方式(文本、图片、链接) * @param shareType 分享的类型(朋友圈,会话) */ public void shareByWebchat(ShareContent shareContent, int shareType){ if(checkInstall()){ 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; } } }/** * 检查是否安装微信 * @return */ private boolean checkInstall() { if(mWXApi.isWXAppInstalled()){ return true; }else { Toast.makeText(mContext,"请安装微信",Toast.LENGTH_SHORT).show(); return false; } }private abstract class ShareContent { protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract int getPictureResource(); protected abstract String getPictureUrl(); }/** * 设置分享文字的内容 * @author chengcj1 * */ public class ShareContentText extends ShareContent { private String content; /** * 构造分享文字类 * @param content 分享的文字内容 */ 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; }@Override protected String getPictureUrl() { return null; } }/* * 获取文本分享对象 如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ public ShareContent getShareContentText(String content) { // if (mShareContentText == null) { mShareContentText = new ShareContentText(content); // } return (ShareContentText) mShareContentText; }/** * 设置分享图片的内容 * @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 getPictureUrl() { return null; } @Override protected String getContent() { return null; }@Override protected String getTitle() { return null; }@Override protected String getURL() { return null; } }/* * 获取图片分享对象如果你每次想要分享的需要不同的内容就别进行 if 判断不然会复用之前的信息 */ public ShareContent getShareContentPicture(int pictureResource) { // if (mShareContentPicture == null) { mShareContentPicture = new ShareContentPicture(pictureResource); // } return (ShareContentPicture) mShareContentPicture; }/** * 设置分享链接的内容 * @author chengcj1 * */ public class ShareContentWebpage extends ShareContent { private String title; private String content; private String url; private int pictureResource; private String pictureUrl; public ShareContentWebpage(String title, String content, String url,String pictureUrl){ this.title = title; this.content = content; this.url = url; this.pictureUrl = pictureUrl; } 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; }@Override protected String getPictureUrl() { return pictureUrl; } }/* * 获取网页分享对象如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ public ShareContent getShareContentWebpag(String title, String content, String url,String pictureLocalUrl) { //if (mShareContentWebpag == null) { mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureLocalUrl); //} return (ShareContentWebpage) mShareContentWebpag; }/** * 设置分享视频的内容 * @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; }@Override protected String getPictureUrl() { return null; } }/* * 获取视频分享内容 如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ 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(); //初始化一个WXTextObject对象 WXTextObject textObj = new WXTextObject(); textObj.text = text; //用WXTextObject对象初始化一个WXMediaMessage对象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; //构造一个Req SendMessageToWX.Req req = new SendMessageToWX.Req(); //transaction字段用于唯一标识一个请求 req.transaction = buildTransaction("textshare"); req.message = msg; //发送的目标场景, 可以选择发送到会话 WXSceneSession 或者朋友圈 WXSceneTimeline。 默认发送到会话。 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 = https://www.songbingjia.com/android/Util.bmpToByteArray(thumbBitmap, true); //设置缩略图SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }/* * 分享链接,因为我这里设置的图片是网络图片(http://开头),因为android是不能在主线程中调用请求网络的耗时操作的,所以加了线程 */ private void shareWebPage(final ShareContent shareContent, final int shareType) { new Thread(new Runnable() { @Override public void run() { try { 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()); //这里调用的是本工程下的资源文件下的图片,如果是本地图片是不用加线程的,如:R.drawable.ic_languner Bitmap bmp = BitmapFactory.decodeStream(new URL(shareContent.getPictureUrl() + "?x-oss-process=image/resize,m_fixed,h_60,w_60").openStream()); //这里是看官方dome中分享网络图片后改的 Bitmap thumb = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); if (thumb == null) { Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = https://www.songbingjia.com/android/Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }catch (Exception e){ e.printStackTrace(); } } }).start(); }/* * 分享视频 */ 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.mipmap.ic_launcher); //BitmapFactory.decodeStream(new URL(video.videoUrl).openStream()); /** * 测试过程中会出现这种情况,会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制,所以有可能是大小超过了默认的范围。 * 一般情况下缩略图超出比较常见。Title、description都是文本,一般不会超过。 */ Bitmap thumbBitmap =Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true); thumb.recycle(); msg.thumbData = https://www.songbingjia.com/android/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(); } }
3.微信分享的使用
private WechatShareManager mShareManager; /** * 微信分享 按照个人需求更改 我这里是自定义了一个model类来传分享的内容,而且这里只用到了分享链接 * @param isFriend 是否是朋友圈的分享 */ private void WChatShare(boolean isFriend){ BeanShareInfo shareInfo = (BeanShareInfo) this.info; WechatShareManager.ShareContentWebpage shareWebpage = (WechatShareManager.ShareContentWebpage) mShareManager.getShareContentWebpag(shareInfo.getTitle(),shareInfo.getDescription(),shareInfo.getTargetUrl(),shareInfo.getShareCoverUrl()); if(isFriend){//朋友圈 mShareManager.shareByWebchat(shareWebpage,WechatShareManager.WECHAT_SHARE_TYPE_FRENDS); }else{ mShareManager.shareByWebchat(shareWebpage,WechatShareManager.WECHAT_SHARE_TYPE_TALK); } }
4.集成微信登录
首先做的还是上文第1点的内容
然后开始代码
import android.app.Activity; import推荐阅读
- Android四大组件之Service
- Android网络编程之——文件断点下载(暂停/继续/重新下载)
- android 垃圾回收机制
- 关于Android软键盘把布局顶上去的问题
- android4.3 Bluetooth(le)分析之startLeScan分析
- android4.3 Bluetooth分析之扫描分析
- android:process=":remote"属性解说
- Android中的多线程开源框架
- Android开发(《Gradle Recipes for Android》阅读笔记(翻译)4.2——增加自定义task)