首页 > 代码库 > Android实现微博分享及其注意事项

Android实现微博分享及其注意事项

微博的参考文档在开放平台上转悠了半天,没有找到,最终才惊奇的发现在SDK的Demo中,还是我的老思路,遇到这种第三方接入的最快最好的入手点就是入门文档和官方Demo,打开Demo我们就会看到相关文档和源码,由于前面的APPID的申请和QQ、微信差不多,所以我在这里就不重复了,可以参考一下上面的两篇文章。接下来我就将我封装好的源码粘出来如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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();
    }
}

 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android实现微博分享及其注意事项