首页 > 代码库 > Android实现QQ分享及注意事项
Android实现QQ分享及注意事项
一、获取APPID和帮助文档
在前面我介绍了关于Android中微信分享的文章《 Android实现微信分享及注意事项》这一篇文章来看看关于QQ分享。
可以参看新手引导和接入说明:http://wiki.open.qq.com/wiki/移动应用接入wiki索引
分享相关文档说明:http://wiki.open.qq.com/index.php?title=Android_API调用说明&=45038#1.13_.E5.88.86.E4.BA.AB.E6.B6.88.E6.81.AF.E5.88.B0QQ.EF.BC.88.E6.97.A0.E9.9C.80QQ.E7.99.BB.E5.BD.95.EF.BC.89
二、实现代码(转载请说明出处:http://blog.csdn.net/dawanganban )
public class QQShareManager { /** * 链接 */ public static final int QQ_SHARE_WAY_WEBPAGE = 3; /** * QQ */ public static final int QQ_SHARE_TYPE_TALK = 1; /** * QQ空间 */ public static final int QQ_SHARE_TYPE_ZONE = 2; /** * 分享成功 */ public static final int CALLBACK_CODE_SUCCESS = 0; /** * 取消分享 */ public static final int CALLBACK_CODE_CANCEL = 1; /** * 拒绝访问 */ public static final int CALLBACK_CODE_DENY = 2; /** * 未知 */ public static final int CALLBACK_CODE_UNKNOWN = 3; private static String appId; private Tencent mTencent; private QQShare qqShare; private QzoneShare qzoneShare; private QQShareResponse qqShareResponse; public void registShare(Context context){ //初始化数据 if(appId == null){ appId = QQShareUtil.getQQAppId(context); } //初始化分享代码 if(appId != null && (qqShare == null || qzoneShare == null)){ mTencent = Tencent.createInstance(appId, context); qqShare = new QQShare(context, mTencent.getQQToken()); qzoneShare = new QzoneShare(context, mTencent.getQQToken()); } } /** * 分享qq和空间 * @param shareContent 分享内容 * @param shareType 选择类型(qq、空间) */ public void shareByQQ(Activity activity, ShareContent shareContent, int shareType){ shareWebPage(activity, shareType, shareContent); } private void shareWebPage(Activity activity, int shareType, ShareContent shareContent){ Bundle params = new Bundle(); if(shareType == QQ_SHARE_TYPE_ZONE){ shareWebPageQzone(activity, shareContent, params); }else{ shareWebPageQQ(activity, shareContent, params); } } private void shareWebPageQQ(Activity activity, ShareContent shareContent, Bundle params) { params.putString(QQShare.SHARE_TO_QQ_TITLE, shareContent.getTitle()); params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareContent.getContent()); params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT); params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareContent.getURL()); params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareContent.getPicUrl()); doShareToQQ(activity, params, iUiListener); } private void shareWebPageQzone(Activity activity, ShareContent shareContent, Bundle params) { params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareContent.getTitle()); params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareContent.getContent()); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareContent.getURL()); ArrayList<String> imageUrls = new ArrayList<String>(); imageUrls.add(shareContent.getPicUrl()); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls); //params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, shareContent.getPicUrl()); doShareToQzone(activity, params, iUiListener); } private void doShareToQQ(final Activity activity, final Bundle params, final IUiListener iUiListener) { new Thread(new Runnable() { @Override public void run() { if(qqShare != null){ qqShare.shareToQQ(activity, params, iUiListener); } } }).start(); } private void doShareToQzone(final Activity activity, final Bundle params, final IUiListener iUiListener) { new Thread(new Runnable() { @Override public void run() { if(qzoneShare != null){ qzoneShare.shareToQzone(activity, params, iUiListener); } } }).start(); } private final IUiListener iUiListener = new IUiListener() { @Override public void onCancel() { sendRespCode(CALLBACK_CODE_CANCEL); } @Override public void onComplete(Object response) { sendRespCode(CALLBACK_CODE_SUCCESS); } @Override public void one rror(UiError e) { sendRespCode(CALLBACK_CODE_DENY); } private void sendRespCode(int code) { if(qqShareResponse != null){ qqShareResponse.respCode(code); } } }; public interface QQShareResponse{ /** * 分享结果 * @param code 结果码 */ public void respCode(int code); } /** * 注册结果回馈 * @param qqShareResponse */ public void setOnQQShareResponse(QQShareResponse qqShareResponse){ this.qqShareResponse = qqShareResponse; } /** * 欢迎关注-阳光小强-http://blog.csdn.net/dawanganban * @author lixiaoqiang * */ private abstract class ShareContent{ protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract String getPicUrl(); } /** * 设置分享链接的内容 * @author Administrator * */ public class ShareContentWebpage extends ShareContent{ private String title; private String content; private String url; private String picUrl; public ShareContentWebpage(String title, String content, String url, String picUrl){ this.title = title; this.content = content; this.url = url; this.picUrl = picUrl; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return title; } @Override protected String getURL() { return url; } @Override protected int getShareWay() { return QQ_SHARE_WAY_WEBPAGE; } @Override protected String getPicUrl() { return picUrl; } } }
上面实现了文字分享、图片分享和链接分享接口,希望对正在学习Android的朋友和需要的朋友有所帮助。
另外部分细节问题和微信分享几乎一样,可以参考《 Android实现微信分享及注意事项》
Android实现QQ分享及注意事项
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。