Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現QQ分享及注意事項

Android實現QQ分享及注意事項

編輯:關於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());
		ArrayList imageUrls = 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實現微信分享及注意事項》

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved