Android第三期 - sharesdk社会化分享组件
网站分享比较简单的一句js就可以了,但是手机就没有这么简单了,要有点小复杂,刚学Android两个月了,也再用第三方的Android开发组件,现在介绍给大家sharesdk。直接上代码!!
第一步:下载ShareSDK
官网 http://share.sharesdk.cn/Download
第二步:集成ShareSDK
进入ShareSDK解压目录,打开“Share SDK for Android”目录,可以找到“QuickIntegrater.jar”,这个就是快速集成ShareSDK的工具
Windows下
首先确定已经安装了JDK,并且正确配置了JAVA_HOME和PATH系统变量,此时可以双击QuickIntegrater启动程序
Linux/Mac OS下
同样需要先确保已经正确配置了JDK,之后启动终端,cd进入“Share SDK for Android”目录,输入: java -jar QuickIntegrater.jar 可启动QuickIntegrater
正确输入项目的名称和包名,勾选需要的集成的平台,然后点击“确定”。QuickIntegrater会自行产生一个目录,复制此目录中的文件到您的项目中覆盖即可。
第三步:配置AndroidManifest.xml
1、添加权限
|
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
|
<
uses-permission
android:name
=
"android.permission.GET_TASKS"
/>
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_WIFI_STATE"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
/>
<
uses-permission
android:name
=
"android.permission.CHANGE_WIFI_STATE"
/>
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
=
"android.permission.READ_PHONE_STATE"
/>
<
uses-permission
android:name
=
"android.permission.MANAGE_ACCOUNTS"
/>
<
uses-permission
android:name
=
"android.permission.GET_ACCOUNTS"
/>
2、添加activity信息
<
activity
android:name
=
"cn.sharesdk.framework.ShareSDKUIShell"
android:theme
=
"@android:style/Theme.Translucent.NoTitleBar"
android:configChanges
=
"keyboardHidden|orientation|screenSize"
android:screenOrientation
=
"portrait"
android:windowSoftInputMode
=
"stateHidden|adjustResize"
>
<
intent-filter
>
<
data
android:scheme
=
"tencent100371282"
/>
<
action
android:name
=
"android.intent.action.VIEW"
/>
<
category
android:name
=
"android.intent.category.BROWSABLE"
/>
<
category
android:name
=
"android.intent.category.DEFAULT"
/>
</
intent-filter
>
</
activity
>
3、如果您集成了微信或者易信,还需要添加下面两个Activity
<!--微信分享回调 -->
<
activity
android:name
=
".wxapi.WXEntryActivity"
android:theme
=
"@android:style/Theme.Translucent.NoTitleBar"
android:configChanges
=
"keyboardHidden|orientation|screenSize"
android:exported
=
"true"
android:screenOrientation
=
"portrait"
/>
<!--易信分享回调 -->
<
activity
android:name
=
".yxapi.YXEntryActivity"
android:theme
=
"@android:style/Theme.Translucent.NoTitleBar"
android:configChanges
=
"keyboardHidden|orientation|screenSize"
android:exported
=
"true"
android:screenOrientation
=
"portrait"
/>
|
第四步:添加分享代码
在您的代码中调用此方法,即可打开一键分享功能进行分享
|
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
|
Private
void
showShare() {
ShareSDK.initSDK(
this
);
OnekeyShare oks =
new
OnekeyShare();
//关闭sso授权
oks.disableSSOWhenAuthorize();
// 分享时Notification的图标和文字
oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
// title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用
oks.setTitle(getString(R.string.share));
// titleUrl是标题的网络链接,仅在人人网和QQ空间使用
oks.setTitleUrl(
"http://sharesdk.cn"
);
// text是分享文本,所有平台都需要这个字段
oks.setText(
"我是分享文本"
);
// imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
oks.setImagePath(
"/sdcard/test.jpg"
);
// url仅在微信(包括好友和朋友圈)中使用
oks.setUrl(
"http://sharesdk.cn"
);
// comment是我对这条分享的评论,仅在人人网和QQ空间使用
oks.setComment(
"我是测试评论文本"
);
// site是分享此内容的网站名称,仅在QQ空间使用
oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址,仅在QQ空间使用
oks.setSiteUrl(
"http://sharesdk.cn"
);
// 启动分享GUI
oks.show(
this
);
}
|
我给大家写了一个demo,官网的demo功能太多了,像我一样的初学估计看起来会有点吃力,我把里面简单的拆分开来,这样大家会有一个清晰的思路。
将定制的导入自己的项目后,然后写MainActivity部分:
|
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
|
package
com.share;
import
cn.sharesdk.framework.ShareSDK;
import
cn.sharesdk.onekeyshare.OnekeyShare;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity {
private
TextView shareall;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// showShare();
shareall = (TextView) findViewById(R.id.shareall);
shareall.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
// TODO Auto-generated method stub
showShare();
}
});
}
private
void
showShare() {
ShareSDK.initSDK(
this
);
OnekeyShare oks =
new
OnekeyShare();
// 关闭sso授权
oks.disableSSOWhenAuthorize();
// 分享时Notification的图标和文字
oks.setNotification(R.drawable.ic_launcher, getString(R.string.ctone));
// title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用
oks.setTitle(getString(R.string.share));
// titleUrl是标题的网络链接,仅在人人网和QQ空间使用
oks.setTitleUrl(
"http://www.ctone.net/"
);
// text是分享文本,所有平台都需要这个字段
oks.setText(
"精彩无限,融通未来。 ----郑州ctone科技,官网http://www.ctone.net/"
);
// imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
//oks.setImagePath("http://img0.bdstatic.com/img/image/shouye/muzrxc1.jpg");
// url仅在微信(包括好友和朋友圈)中使用
oks.setUrl(
"http://www.ctone.net/"
);
// comment是我对这条分享的评论,仅在人人网和QQ空间使用
oks.setComment(
"亲,留个言吧!"
);
// site是分享此内容的网站名称,仅在QQ空间使用
oks.setSite(getString(R.string.ctone));
// siteUrl是分享此内容的网站地址,仅在QQ空间使用
oks.setSiteUrl(
"http://www.ctone.net/"
);
// 启动分享GUI
oks.show(
this
);
}
}
|
activitymain.xml部分:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
".MainActivity"
android:background
=
"@color/white"
>
<
TextView
android:id
=
"@+id/shareall"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:gravity
=
"center"
android:text
=
"@string/hello_world"
/>
</
RelativeLayout
>
|
assets部分:share.xml
|
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
DevInfor
>
<!--
说明:
1、表格中的第一项
<ShareSDK
AppKey="api20" />
是必须的,其中的AppKey是您在ShareSDK上注册的开发者帐号的AppKey
2、所有集成到您项目的平台都应该为其在表格中填写相对应的开发者信息,以新浪微博为例:
<SinaWeibo
Id="1"
SortId="1"
AppKey="568898243"
AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"
RedirectUrl="http://www.sharesdk.cn"
Enable="true" />
其中的SortId是此平台在分享列表中的位置,由开发者自行定义,可以是任何整型数字,数值越大
越靠后AppKey、AppSecret和RedirectUrl是您在新浪微博上注册开发者信息和应用后得到的信息
Id是一个保留的识别符,整型,ShareSDK不使用此字段,供您在自己的项目中当作平台的识别符。
Enable字段表示此平台是否有效,布尔值,默认为true,如果Enable为false,即便平台的jar包
已经添加到应用中,平台实例依然不可获取。
各个平台注册应用信息的地址如下:
新浪微博 http://open.weibo.com
腾讯微博 http://dev.t.qq.com
QQ空间 http://connect.qq.com/intro/login/
微信好友 http://open.weixin.qq.com
Facebook https://developers.facebook.com
Twitter https://dev.twitter.com
人人网 http://dev.renren.com
开心网 http://open.kaixin001.com
搜狐微博 http://open.t.sohu.com
网易微博 http://open.t.163.com
豆瓣 http://developers.douban.com
有道云笔记 http://note.youdao.com/open/developguide.html#app
印象笔记 https://dev.evernote.com/
Linkedin https://www.linkedin.com/secure/developer?newapp=
FourSquare https://developer.foursquare.com/
搜狐随身看 https://open.sohu.com/
Flickr http://www.flickr.com/services/
Pinterest http://developers.pinterest.com/
Tumblr http://www.tumblr.com/developers
Dropbox https://www.dropbox.com/developers
Instagram http://instagram.com/developer#
VKontakte http://vk.com/dev
-->
<
ShareSDK
AppKey = "androidv1101"/>
<!-- 修改成你在sharesdk后台注册的应用的appkey"-->
<!-- ShareByAppClient标识是否使用微博客户端分享,默认是false -->
<
SinaWeibo
Id
=
"1"
SortId
=
"1"
AppKey
=
"568898243"
AppSecret
=
"38a4f8204cc784f81f9f0daaf31e02e3"
RedirectUrl
=
"http://www.sharesdk.cn"
ShareByAppClient
=
"true"
Enable
=
"true"
/>
<
TencentWeibo
Id
=
"2"
SortId
=
"2"
AppKey
=
"801307650"
AppSecret
=
"ae36f4ee3946e1cbb98d6965b0b2ff5c"
RedirectUri
=
"http://sharesdk.cn"
Enable
=
"true"
/>
<!-- ShareByAppClient标识是否使用微博客户端分享,默认是false -->
<
QZone
Id
=
"3"
SortId
=
"3"
AppId
=
"100371282"
AppKey
=
"aed9b0303e3ed1e27bae87c33761161d"
ShareByAppClient
=
"true"
Enable
=
"true"
/>
<!--
Wechat微信和WechatMoments微信朋友圈的appid是一样的;
注意:开发者不能用我们这两个平台的appid,否则分享不了
微信测试的时候,微信测试需要先签名打包出apk,
sample测试微信,要先签名打包,keystore在sample项目中,密码123456
BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过
审核的应用也可以执行分享,但是仅限于分享文字和图片,不能分享其他类型,
默认值为false。此外,微信收藏不支持此字段。
-->
<
Wechat
Id
=
"4"
SortId
=
"4"
AppId
=
"wx4868b35061f87885"
BypassApproval
=
"true"
Enable
=
"true"
/>
<
WechatMoments
Id
=
"5"
SortId
=
"5"
AppId
=
"wx4868b35061f87885"
BypassApproval
=
"true"
Enable
=
"true"
/>
<
WechatFavorite
Id
=
"6"
SortId
=
"6"
AppId
=
"wx4868b35061f87885"
Enable
=
"true"
/>
<
QQ
Id
=
"7"
SortId
=
"7"
AppId
=
"100371282"
AppKey
=
"aed9b0303e3ed1e27bae87c33761161d"
Enable
=
"true"
/>
<
Facebook
Id
=
"8"
SortId
=
"8"
ConsumerKey
=
"107704292745179"
ConsumerSecret
=
"38053202e1a5fe26c80c753071f0b573"
Enable
=
"true"
/>
<
Twitter
Id
=
"9"
SortId
=
"9"
ConsumerKey
=
"mnTGqtXk0TYMXYTN7qUxg"
ConsumerSecret
=
"ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"
CallbackUrl
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
Renren
Id
=
"10"
SortId
=
"10"
AppId
=
"226427"
ApiKey
=
"fc5b8aed373c4c27a05b712acba0f8c3"
SecretKey
=
"f29df781abdd4f49beca5a2194676ca4"
Enable
=
"true"
/>
<
KaiXin
Id
=
"11"
SortId
=
"11"
AppKey
=
"358443394194887cee81ff5890870c7c"
AppSecret
=
"da32179d859c016169f66d90b6db2a23"
RedirectUri
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
Email
Id
=
"12"
SortId
=
"12"
Enable
=
"true"
/>
<
ShortMessage
Id
=
"13"
SortId
=
"13"
Enable
=
"true"
/>
<
SohuMicroBlog
Id
=
"14"
SortId
=
"14"
ApiKey
=
"q70QBQM9T0COxzYpGLj9"
ConsumerKey
=
"q70QBQM9T0COxzYpGLj9"
ConsumerSecret
=
"XXYrx%QXbS!uA^m2$8TaD4T1HQoRPUH0gaG2BgBd"
CallbackUrl
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
NetEaseMicroBlog
Id
=
"15"
SortId
=
"15"
ConsumerKey
=
"T5EI7BXe13vfyDuy"
ConsumerSecret
=
"gZxwyNOvjFYpxwwlnuizHRRtBRZ2lV1j"
RedirectUri
=
"http://www.shareSDK.cn"
Enable
=
"true"
/>
<
Douban
Id
=
"16"
SortId
=
"16"
ApiKey
=
"02e2cbe5ca06de5908a863b15e149b0b"
Secret
=
"9f1e7b4f71304f2f"
RedirectUri
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
YouDao
Id
=
"17"
SortId
=
"17"
HostType
=
"product"
ConsumerKey
=
"dcde25dca105bcc36884ed4534dab940"
ConsumerSecret
=
"d98217b4020e7f1874263795f44838fe"
RedirectUri
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
SohuSuishenkan
Id
=
"18"
SortId
=
"18"
AppKey
=
"e16680a815134504b746c86e08a19db0"
AppSecret
=
"b8eec53707c3976efc91614dd16ef81c"
RedirectUri
=
"http://sharesdk.cn"
Enable
=
"true"
/>
<!--
在中国大陆,印象笔记有两个服务器,一个是沙箱(sandbox),一个是生产服务器(china)。
一般你注册应用,它会先让你使用sandbox,当你完成测试以后,可以到
http://dev.yinxiang.com/support/上激活你的ConsumerKey,激活成功后,修改HostType
为china就好了。至于如果您申请的是国际版的印象笔记(Evernote),则其生产服务器类型为
“product”。
如果目标设备上已经安装了印象笔记客户端,ShareSDK允许应用调用本地API来完成分享,但
是需要将应用信息中的“ShareByAppClient”设置为true,此字段默认值为false。
-->
<
Evernote
Id
=
"19"
SortId
=
"19"
HostType
=
"sandbox"
ConsumerKey
=
"sharesdk-7807"
ConsumerSecret
=
"d05bf86993836004"
ShareByAppClient
=
"false"
Enable
=
"true"
/>
<
LinkedIn
Id
=
"20"
SortId
=
"20"
ApiKey
=
"ejo5ibkye3vo"
SecretKey
=
"cC7B2jpxITqPLZ5M"
RedirectUrl
=
"http://sharesdk.cn"
Enable
=
"true"
/>
<
GooglePlus
Id
=
"21"
SortId
=
"21"
Enable
=
"true"
/>
<
FourSquare
Id
=
"22"
SortId
=
"22"
ClientID
=
"G0ZI20FM30SJAJTX2RIBGD05QV1NE2KVIM2SPXML2XUJNXEU"
ClientSecret
=
"3XHQNSMMHIFBYOLWEPONNV4DOTCDBQH0AEMVGCBG0MZ32XNU"
RedirectUrl
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
Pinterest
Id
=
"23"
SortId
=
"23"
ClientId
=
"1432928"
Enable
=
"true"
/>
<
Flickr
Id
=
"24"
SortId
=
"24"
ApiKey
=
"33d833ee6b6fca49943363282dd313dd"
ApiSecret
=
"3a2c5b42a8fbb8bb"
RedirectUri
=
"http://www.sharesdk.cn"
Enable
=
"true"
/>
<
Tumblr
Id
=
"25"
SortId
=
"25"
OAuthConsumerKey
=
"2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM"
SecretKey
=
"3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo"
CallbackUrl
=
"http://sharesdk.cn"
Enable
=
"true"
/>
<
Dropbox
Id
=
"26"
SortId
=
"26"
AppKey
=
"7janx53ilz11gbs"
AppSecret
=
"c1hpx5fz6tzkm32"
Enable
=
"true"
/>
<
VKontakte
Id
=
"27"
SortId
=
"27"
ApplicationId
=
"3921561"
Enable
=
"true"
/>
<
Instagram
Id
=
"28"
SortId
=
"28"
ClientId
=
"ff68e3216b4f4f989121aa1c2962d058"
ClientSecret
=
"1b2e82f110264869b3505c3fe34e31a1"
RedirectUri
=
"http://sharesdk.cn"
Enable
=
"true"
/>
<!--
Yixin易信和YixinMoments易信朋友圈的appid是一样的;
注意:开发者不能用我们这两个平台的appid,否则分享不了
易信测试的时候需要先签名打包出apk,
sample测试易信,要先签名打包,keystore在sample项目中,密码123456
BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过
审核的应用也可以执行分享,但是仅限于分享文字或图片,不能分享其他类型,
默认值为false。
-->
<
Yixin
Id
=
"29"
SortId
=
"29"
AppId
=
"yx0d9a9f9088ea44d78680f3274da1765f"
BypassApproval
=
"true"
Enable
=
"true"
/>
<
YixinMoments
Id
=
"30"
SortId
=
"30"
AppId
=
"yx0d9a9f9088ea44d78680f3274da1765f"
BypassApproval
=
"true"
Enable
=
"true"
/>
<
Mingdao
Id
=
"31"
SortId
=
"31"
AppKey
=
"EEEE9578D1D431D3215D8C21BF5357E3"
AppSecret
=
"5EDE59F37B3EFA8F65EEFB9976A4E933"
RedirectUri
=
"http://sharesdk.cn"
Enable
=
"true"
/>
</
DevInfor
>
|
附效果图:
祝大家好好工作,好好活着。






