写了一个简单的分享demo,创建一个button按钮,点击按钮弹出分享窗口,选择需要分享的平台进行分享
1. 获取友盟Appkey
如果你之前已经在友盟注册了应用,并获取到了Appkey,可以继续使用它.
如果你尚未在友盟注册开发者账号,需要先注册,注册之后登录你的账号,点击添加新应用,填写完应用基本信息后,将进入”下载SDK并添加代码”页面,此页面即可得到Appkey。友盟文档中心http://dev.umeng.com/
2. 下载并安装SDK
下载SDK最新版
添加代码和资源引用,我们提供了添加资源文件和jar文件的两种方式,可以根据需求选择
解压SDK压缩包,将文件夹中的’main/libs’和’main/res’文件夹复制到你的项目工程根目录下(如使用’ADT 17’以下用户需要手动添加’libs’下的jar文件到工程Path中)
解压SDK压缩包,将文件夹中的social_sdk_library_project文件夹导入Eclipse,并在您的工程中添加对此项目的引用即可。
AndroidManifest配置(需要在各平台创建应用获取相应的appkey)
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- renren sso Permission for Unit Test -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<!-- 友盟AppKey, 需要替换成你自己的 -->
<meta-data android:name="UMENG_APPKEY" android:value="4eaee02c527015373b000003" >
</meta-data>
<!-- 4eaee02c527015373b000003 -->
<!-- android:value="52a98cf856240b902211203e" > -->
<!-- ************************* 分享相关的注册 START **************************** -->
<!-- 分享内容编辑页 -->
<activity android:name="com.umeng.socialize.view.ShareActivity" android:configChanges="orientation|keyboard" android:launchMode="singleTask" android:noHistory="true" android:theme="@style/Theme.UMDialog" android:windowSoftInputMode="stateVisible|adjustResize" >
</activity>
<!-- 腾讯SSO授权的Activity注册 -->
<activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tencent100424468" />
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 微信回调activity -->
<activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 易信回调activity -->
<activity android:name=".yxapi.YXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 人人SSO授权所需的OAuthActivity -->
<activity android:name="com.renn.rennsdk.oauth.OAuthActivity" android:configChanges="orientation|navigation|keyboardHidden" />
<!-- facebook相关 -->
<activity android:name=".activity.FacebookActivity" >
</activity>
<activity android:name="com.facebook.LoginActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- ************************* 分享相关的注册 END **************************** -->
<!-- #######添加UmengAppkey######## -->
<meta-data android:name="UMENG_APPKEY" android:value="56f5034a67e58e27f50007b3" >
</meta-data>
</application>
如果你把需要的sdk都导入工程了,AndroidManifast.xml也配置好了,接下来就是代码实现部分,当点击button的时候弹出分享窗口
private UMSocialService mController = UMServiceFactory
.getUMSocialService("com.umeng.share");;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_share);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//添加所有的平台
addCustomPlatforms();
}
});
// 配置需要分享的相关平台
configPlatforms();
// 设置分享的内容
setShareContent();
添加所有的平台,把需要分享的平台都添加上并配置好
/** * 添加所有的平台</br> */
private void addCustomPlatforms() {
// 添加微信平台
addWXPlatform();
// 添加QQ平台
addQQQZonePlatform();
// 添加印象笔记平台
addEverNote();
// 添加facebook平台
addFacebook();
// 添加Instagram平台
addInstagram();
// 添加来往、来往动态平台
addLaiWang();
// 添加LinkedIn平台
addLinkedIn();
// 添加Pinterest平台
addPinterest();
// 添加Pocket平台
addPocket();
// 添加有道云平台
addYNote();
// 添加易信平台
addYXPlatform();
// 添加短信平台
addSMS();
// 添加email平台
addEmail();
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,
SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,
SHARE_MEDIA.SINA, SHARE_MEDIA.TENCENT, SHARE_MEDIA.DOUBAN,
SHARE_MEDIA.RENREN, SHARE_MEDIA.EMAIL, SHARE_MEDIA.EVERNOTE,
SHARE_MEDIA.FACEBOOK, SHARE_MEDIA.GOOGLEPLUS,
SHARE_MEDIA.INSTAGRAM, SHARE_MEDIA.LAIWANG,
SHARE_MEDIA.LAIWANG_DYNAMIC, SHARE_MEDIA.LINKEDIN,
SHARE_MEDIA.PINTEREST, SHARE_MEDIA.POCKET, SHARE_MEDIA.SMS,
SHARE_MEDIA.TWITTER, SHARE_MEDIA.YIXIN,
SHARE_MEDIA.YIXIN_CIRCLE, SHARE_MEDIA.YNOTE);
mController.openShare(LoginActivity.this, false);
}
由于平台比较多,我这里只提供微信和QQ平台的方法,其他的方法看demo
/** * @功能描述 : 添加微信平台分享 * @return */
private void addWXPlatform() {
// 注意:在微信授权的时候,必须传递appSecret
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appId = "wx967daebe835fbeac";
String appSecret = "5bb696d9ccd75a38c8a0bfe0675559b3";
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(LoginActivity.this, appId,
appSecret);
wxHandler.addToSocialSDK();
// 支持微信朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(LoginActivity.this,
appId, appSecret);
wxCircleHandler.setToCircle(true);
wxCircleHandler.addToSocialSDK();
}
/** * @功能描述 : 添加QQ平台支持 QQ分享的内容, 包含四种类型, 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary, * image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title : * 要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl * : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 ) * @return */
private void addQQQZonePlatform() {
String appId = "100424468";
String appKey = "c7394704798a158208a74ab60104f0ba";
// 添加QQ支持, 并且设置QQ分享内容的target url
UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(LoginActivity.this,
appId, appKey);
qqSsoHandler.setTargetUrl("http://www.umeng.com");
qqSsoHandler.addToSocialSDK();
// 添加QZone平台
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(
LoginActivity.this, appId, appKey);
qZoneSsoHandler.addToSocialSDK();
}
配置分享平台的参数
/** * 配置分享平台参数 */
private void configPlatforms() {
// 添加新浪sso授权
mController.getConfig().setSsoHandler(new SinaSsoHandler());
// 添加腾讯微博SSO授权
mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
// 添加人人网SSO授权
RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(
MainActivity.this, "201874",
"28401c0964f04a72a14c812d6132fcef",
"3bf66e42db1e4fa9829b955cc300b737");
mController.getConfig().setSsoHandler(renrenSsoHandler);
// 添加QQ、QZone平台
addQQQZonePlatform();
// 添加微信、微信朋友圈平台
addWXPlatform();
}
根据不同的平台设置不同的分享内容
/** * 根据不同的平台设置不同的分享内容 */
private void setShareContent() {
// 配置SSO
mController.getConfig().setSsoHandler(new SinaSsoHandler());
mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(
MainActivity.this, "100424468",
"c7394704798a158208a74ab60104f0ba");
qZoneSsoHandler.addToSocialSDK();
mController.setShareContent("友盟社会化组件(SDK)让移动应用快速整合社交分享功能");
// APP ID:201874, API
// * KEY:28401c0964f04a72a14c812d6132fcef, Secret
// * Key:3bf66e42db1e4fa9829b955cc300b737.
RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(
MainActivity.this, "201874",
"28401c0964f04a72a14c812d6132fcef",
"3bf66e42db1e4fa9829b955cc300b737");
mController.getConfig().setSsoHandler(renrenSsoHandler);
UMImage localImage = new UMImage(MainActivity.this, R.drawable.ic_launcher);
UMImage urlImage = new UMImage(MainActivity.this,
"http://www.umeng.com/images/pic/social/integrated_3.png");
// UMImage resImage = new UMImage(MainActivity.this, R.drawable.icon);
// 视频分享
UMVideo video = new UMVideo(
"http://v.youku.com/v_show/id_XNTc0ODM4OTM2.html");
// vedio.setThumb("http://www.umeng.com/images/pic/home/social/img-1.png");
video.setTitle("友盟社会化组件视频");
video.setThumb(urlImage);
UMusic uMusic = new UMusic(
"http://music.huoxing.com/upload/20130330/1364651263157_1085.mp3");
uMusic.setAuthor("umeng");
uMusic.setTitle("天籁之音");
uMusic.setThumb(urlImage);
// uMusic.setThumb("http://www.umeng.com/images/pic/social/chart_1.png");
WeiXinShareContent weixinContent = new WeiXinShareContent();
weixinContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,微信");
weixinContent.setTitle("友盟社会化分享组件-微信");
weixinContent.setTargetUrl("http://www.umeng.com");
weixinContent.setShareMedia(urlImage);
mController.setShareMedia(weixinContent);
// 设置朋友圈分享的内容
CircleShareContent circleMedia = new CircleShareContent();
circleMedia.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,朋友圈");
circleMedia.setTitle("友盟社会化分享组件-朋友圈");
circleMedia.setShareImage(urlImage);
// circleMedia.setShareMedia(uMusic);
// circleMedia.setShareMedia(video);
circleMedia.setTargetUrl("http://www.umeng.com");
mController.setShareMedia(circleMedia);
// 设置renren分享内容
RenrenShareContent renrenShareContent = new RenrenShareContent();
renrenShareContent.setShareContent("人人分享内容");
UMImage image = new UMImage(MainActivity.this,
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
image.setTitle("thumb title");
image.setThumb("http://www.umeng.com/images/pic/social/integrated_3.png");
renrenShareContent.setShareImage(image);
renrenShareContent.setAppWebSite("http://www.umeng.com/social");
mController.setShareMedia(renrenShareContent);
UMImage qzoneImage = new UMImage(MainActivity.this,
"http://www.umeng.com/images/pic/social/integrated_3.png");
qzoneImage
.setTargetUrl("http://www.umeng.com/images/pic/social/integrated_3.png");
// 设置QQ空间分享内容
QZoneShareContent qzone = new QZoneShareContent();
qzone.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能 -- QZone");
qzone.setTargetUrl("http://www.umeng.com/social");
qzone.setTitle("QZone title");
qzone.setShareImage(urlImage);
mController.setShareMedia(qzone);
video.setThumb(new UMImage(MainActivity.this, BitmapFactory
.decodeResource(getResources(), R.drawable.ic_logo)));
QQShareContent qqShareContent = new QQShareContent();
qqShareContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能 -- QQ");
qqShareContent.setTitle("hello, title");
qqShareContent.setShareImage(urlImage);
// qqShareContent.setShareMusic(uMusic);
// qqShareContent.setShareVideo(video);
qqShareContent.setTargetUrl("http://www.umeng.com/social");
mController.setShareMedia(qqShareContent);
// 视频分享
UMVideo umVideo = new UMVideo(
"http://v.youku.com/v_show/id_XNTc0ODM4OTM2.html");
umVideo.setThumb("http://www.umeng.com/images/pic/home/social/img-1.png");
umVideo.setTitle("友盟社会化组件视频");
TencentWbShareContent tencent = new TencentWbShareContent();
tencent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,腾讯微博");
// 设置tencent分享内容
mController.setShareMedia(tencent);
// 设置邮件分享内容, 如果需要分享图片则只支持本地图片
MailShareContent mail = new MailShareContent(localImage);
mail.setTitle("share form umeng social sdk");
mail.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,email");
// 设置tencent分享内容
mController.setShareMedia(mail);
// 设置短信分享内容
SmsShareContent sms = new SmsShareContent();
sms.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,短信");
sms.setShareImage(urlImage);
mController.setShareMedia(sms);
SinaShareContent sinaContent = new SinaShareContent(urlImage);
sinaContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,新浪微博");
mController.setShareMedia(sinaContent);
TwitterShareContent twitterShareContent = new TwitterShareContent();
twitterShareContent
.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,TWITTER");
twitterShareContent.setShareMedia(localImage);
mController.setShareMedia(twitterShareContent);
GooglePlusShareContent googlePlusShareContent = new GooglePlusShareContent();
googlePlusShareContent
.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,G+");
googlePlusShareContent.setShareMedia(localImage);
mController.setShareMedia(googlePlusShareContent);
// 来往分享内容
LWShareContent lwShareContent = new LWShareContent();
// lwShareContent.setShareImage(urlImage);
// lwShareContent.setShareMedia(uMusic);
lwShareContent.setShareMedia(umVideo);
lwShareContent.setTitle("友盟社会化分享组件-来往");
lwShareContent.setMessageFrom("友盟分享组件");
lwShareContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,来往");
mController.setShareMedia(lwShareContent);
// 来往动态分享内容
LWDynamicShareContent lwDynamicShareContent = new LWDynamicShareContent();
// lwDynamicShareContent.setShareImage(urlImage);
// lwDynamicShareContent.setShareMedia(uMusic);
lwDynamicShareContent.setShareMedia(umVideo);
lwDynamicShareContent.setTitle("友盟社会化分享组件-来往动态");
lwDynamicShareContent.setMessageFrom("来自友盟");
lwDynamicShareContent.setShareContent("来往动态分享测试");
lwDynamicShareContent.setTargetUrl("http://www.umeng.com");
mController.setShareMedia(lwDynamicShareContent);
}
源码下载