一、获取APPID和帮助文档
在前面我介绍了关于Android中微信分享的文章《 Android实现微信分享及注意事项》这一篇文章来看看关于QQ分享。
可以参看新手引导和接入说明:http://wiki.open.qq.com/wiki/移动应用接入wiki索引
二、实现代码(转载请说明出处: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 onError(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实现微信分享及注意事项》
原文地址:http://blog.csdn.net/dawanganban/article/details/42015249