編輯:關於Android編程
一、獲取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()); ArrayListimageUrls = new ArrayList (); 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實現微信分享及注意事項》
效果圖: 此程序主要的知識點是:SimpleAdapter本身是不支持網絡圖片的, 如果在Map.put(a,b)中 b為一個Bitmap,程序不會報紅色字體
介紹 做項目到一定龐大的時候就會發現方法數太多,安裝包根本就裝不上去了,這個也不足為奇,我們都知道當方法數目超過65536這個數目限制的時候,擋在2.x的系統上面就會出現
AndroidApp上架所需文件上架准備App文件:1.安裝包2.應用商標:圖片格式PNG,大小512*512,小於800KB。3.應用截圖(4~6張):圖片格式JPG或
有時候,我們的微信會收到一些不認識的人發的廣告,甚至是一些微商的詐騙信息,特別一些微信群上,時不時就會遇到一些做垃圾廣告或者不法分子詐騙的,或者不小心就被加