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

Android實現微博分享及其注意事項

編輯:關於Android編程

在前面我寫了兩篇關於QQ和微信的分享(http://blog.csdn.net/dawanganban/article/details/42015249)(http://blog.csdn.net/dawanganban/article/details/40478317)這一篇我們來看以如何實現微博分享功能。

微博的參考文檔在開放平台上轉悠了半天,沒有找到,最終才驚奇的發現在SDK的Demo中,還是我的老思路,遇到這種第三方接入的最快最好的入手點就是入門文檔和官方Demo,打開Demo我們就會看到相關文檔和源碼,由於前面的APPID的申請和QQ、微信差不多,所以我在這裡就不重復了,可以參考一下上面的兩篇文章。接下來我就將我封裝好的源碼粘出來如下:

public class SinaShareManager{
	/**
	 * 文字
	 */
	public static final int SINA_SHARE_WAY_TEXT = 1;
	/**
	 * 圖片
	 */
	public static final int SINA_SHARE_WAY_PIC = 2;
	/**
	 * 鏈接
	 */	
	public static final int SINA_SHARE_WAY_WEBPAGE = 3;
	
	private static String sinaAppKey;
	public static final String SCOPE = 
	            "email,direct_messages_read,direct_messages_write,"
	            + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
	            + "follow_app_official_microblog," + "invitation_write";
    /** 微博分享的接口實例 */
    private IWeiboShareAPI sinaAPI;
    
    public void registSina(Context context){
    	//獲取appkey
    	if(sinaAppKey == null){
    		sinaAppKey = SinaShareUtil.getSinaAppKey(context);
    	}
    	//初始化微博分享代碼
    	if(sinaAppKey != null){
    		initSinaShare(context);
    	}
    }

	/**
	 * 新浪微博分享方法
	 * @param shareContent 分享的內容
	 */
	public void shareBySina(ShareContent shareContent, Context context){
		if(sinaAPI == null) return;
		switch (shareContent.getShareWay()) {
		case SINA_SHARE_WAY_TEXT:
			shareText(shareContent);
			break;
		case SINA_SHARE_WAY_PIC:
			sharePicture(shareContent, context);
			break;
		case SINA_SHARE_WAY_WEBPAGE:
			shareWebPage(shareContent, context);
			break;
		}
	}
	
	/*
	 * 分享文字
	 */
	private void shareText(ShareContent shareContent){
		//初始化微博的分享消息
		WeiboMessage weiboMessage = new WeiboMessage();
		weiboMessage.mediaObject = getTextObj(shareContent.getContent());
		//初始化從第三方到微博的消息請求
		SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
		request.transaction = buildTransaction("sinatext");
		request.message = weiboMessage;
		//發送請求信息到微博,喚起微博分享界面
		sinaAPI.sendRequest(request);
	}
	
	/*
	 * 分享圖片
	 */
	private void sharePicture(ShareContent shareContent, Context context){
		WeiboMessage weiboMessage = new WeiboMessage();
		weiboMessage.mediaObject = getImageObj(shareContent.getPicResource(), context);
		//初始化從第三方到微博的消息請求
		SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
		request.transaction = buildTransaction("sinatext");
		request.message = weiboMessage;
		//發送請求信息到微博,喚起微博分享界面
		sinaAPI.sendRequest(request);
	}
	
	private void shareWebPage(ShareContent shareContent, Context context){
		WeiboMessage weiboMessage = new WeiboMessage();
		weiboMessage.mediaObject = getWebpageObj(shareContent, context);
		//初始化從第三方到微博的消息請求
		SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
		request.transaction = buildTransaction("sinatext");
		request.message = weiboMessage;
		//發送請求信息到微博,喚起微博分享界面
		sinaAPI.sendRequest(request);
	}
	
	/**
	 * 歡迎關注-陽光小強-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 int getPicResource();
	}
	
	/**
	 * 設置分享文字的內容
	 * @author Administrator
	 *
	 */
	public class ShareContentText extends ShareContent{
		private String content;
		
		/**
		 * 構造分享文字類
		 * @param text 分享的文字內容
		 */
		public ShareContentText(String content){
			this.content = content;
		}

		@Override
		protected String getContent() {

			return content;
		}

		@Override
		protected String getTitle() {
			return null;
		}

		@Override
		protected String getURL() {
			return null;
		}

		@Override
		protected int getPicResource() {
			return -1;
		}

		@Override
		protected int getShareWay() {
			return SINA_SHARE_WAY_TEXT;
		}
		
	}
	
	/**
	 * 設置分享圖片的內容
	 * @author Administrator
	 *
	 */
	public class ShareContentPic extends ShareContent{
		private int picResource;
		public ShareContentPic(int picResource){
			this.picResource = picResource;
		}
		
		@Override
		protected String getContent() {
			return null;
		}

		@Override
		protected String getTitle() {
			return null;
		}

		@Override
		protected String getURL() {
			return null;
		}

		@Override
		protected int getPicResource() {
			return picResource;
		}

		@Override
		protected int getShareWay() {
			return SINA_SHARE_WAY_PIC;
		}
	}
	
	/**
	 * 設置分享鏈接的內容
	 * @author Administrator
	 *
	 */
	public class ShareContentWebpage extends ShareContent{
		private String title;
		private String content;
		private String url;
		private int picResource;
		public ShareContentWebpage(String title, String content, 
				String url, int picResource){
			this.title = title;
			this.content = content;
			this.url = url;
			this.picResource = picResource;
		}

		@Override
		protected String getContent() {
			return content;
		}

		@Override
		protected String getTitle() {
			return title;
		}

		@Override
		protected String getURL() {
			return url;
		}

		@Override
		protected int getPicResource() {
			return picResource;
		}

		@Override
		protected int getShareWay() {
			return SINA_SHARE_WAY_WEBPAGE;
		}
		
	}
	
    /**
     * 創建文本消息對象。
     * 
     * @return 文本消息對象。
     */
    private TextObject getTextObj(String text) {
        TextObject textObject = new TextObject();
        textObject.text = text;
        return textObject;
    }
    
    private ImageObject getImageObj(int picResource, Context context){
    	 ImageObject imageObject = new ImageObject();
    	 Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), picResource);
         imageObject.setImageObject(bmp);
         return imageObject;
    }
    
    private WebpageObject getWebpageObj(ShareContent shareContent, Context context){
    	WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        mediaObject.title = shareContent.getTitle();
        mediaObject.description = shareContent.getContent();
        
        // 設置 Bitmap 類型的圖片到視頻對象裡
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), shareContent.getPicResource());
        mediaObject.setThumbImage(bmp);
        mediaObject.actionUrl = shareContent.getURL();
        mediaObject.defaultText = shareContent.getContent();
        return mediaObject;
    }
	
	private void initSinaShare(Context context){
		// 創建微博 SDK 接口實例
        sinaAPI = WeiboShareSDK.createWeiboAPI(context, sinaAppKey);
        //檢查版本支持情況
        checkSinaVersin(context);
        sinaAPI.registerApp();
	}

	private void checkSinaVersin(final Context context) {
		// 獲取微博客戶端相關信息,如是否安裝、支持 SDK 的版本
        boolean isInstalledWeibo = sinaAPI.isWeiboAppInstalled();
        //int supportApiLevel = sinaAPI.getWeiboAppSupportAPI(); 
        
        // 如果未安裝微博客戶端,設置下載微博對應的回調
        if (!isInstalledWeibo) {
           sinaAPI.registerWeiboDownloadListener(new IWeiboDownloadListener() {
                @Override
                public void onCancel() {
                    Toast.makeText(context, 
                            "取消下載", 
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
	}
	
	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
}
如有疑問可以提出或者添加博客旁邊的QQ群進行交流,希望這三篇文章對大家有所幫助。

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