博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实现微博分享及其注意事项
阅读量:5798 次
发布时间:2019-06-18

本文共 6787 字,大约阅读时间需要 22 分钟。

在前面我写了两篇关于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) { // 获取微博client相关信息。如是否安装、支持 SDK 的版本号 boolean isInstalledWeibo = sinaAPI.isWeiboAppInstalled(); //int supportApiLevel = sinaAPI.getWeiboAppSupportAPI(); // 假设未安装微博client,设置下载微博相应的回调 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群进行交流,希望这三篇文章对大家有所帮助。

你可能感兴趣的文章
mongoose学习笔记1--基础知识1
查看>>
Android Mokoid Open Source Project hacking
查看>>
动态改变actionbar上menu的图标
查看>>
中国科学院国家授时中心
查看>>
如何提高生产力
查看>>
MySQL字符集转换引发插入乱码问题
查看>>
据序和中序序列或者也许为了一个二进制序列,恢复二进制和打印图像(c语言)...
查看>>
S3C2416裸机开发系列19_Fatfs播放录像wav音频文件
查看>>
推荐审稿人:为何需要?如何挑选?
查看>>
Python的高级特性7:闭包和装饰器
查看>>
tail 命令
查看>>
Windows服务安装异常:System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security...
查看>>
ADT后windows菜单未找到Android SDK Manager和Android Virtual Device Manager该解决方案的选择...
查看>>
【MySQL for Mac】在Mac终端导入&导出.sql文件
查看>>
JavaScript的技巧和最佳实践
查看>>
Android开发:shape和selector和layer-list的(详细说明)
查看>>
Aqua Data Studio中文乱码
查看>>
2g-3g
查看>>
Java反射机制示例
查看>>
hdu5461 Largest Point(沈阳网赛)
查看>>