首先你需要注册一个友盟账号,注册之后登录你的账号,点击**添加新应用**,填写完应用基本信息后,将进入"下载SDK并添加代码"页面,此页面即可得到Appkey
。
接着下载SDK,并解压,接着的操作按照官方文档来http://dev.umeng.com/social/android/share/quick-integration
之后就是简单的配置一下清单文件,官方文档上讲的很详细,这里就不多说了,
这里简单说下代码里怎么用
// 首先在您的Activity中添加如下成员变量
final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appID = "wxb8aacb36ca5786bb";
下面是一个工具类,这里只做了 QQ、QQ空间、微信、盆友圈、新浪、人人和腾讯微博的分享
public class ShareDemos {
public static void weixinShareDemo(Context context,UMSocialService mController,String appID,String content,String title,String url){
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(context,appID);
wxHandler.addToSocialSDK();
WeiXinShareContent weixinContent = new WeiXinShareContent();
weixinContent.setShareContent(content);
weixinContent.setTitle(title);
weixinContent.setTargetUrl(url);
// weixinContent.setShareImage(localImage);
mController.setShareMedia(weixinContent);
// 支持微信朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(context,appID);
wxCircleHandler.setToCircle(true);
wxCircleHandler.addToSocialSDK();
CircleShareContent circleMedia = new CircleShareContent();
circleMedia.setShareContent(content);
circleMedia.setTitle(title);
circleMedia.setShareImage(new UMImage(context,
R.drawable.ic_launcher));
circleMedia.setTargetUrl(url);
mController.setShareMedia(circleMedia);
}
public static void conmanShare(Context context,UMSocialService mController,String content,String title,String pic_url){
// 设置分享内容
mController.setShareContent(content+","+pic_url);
// 设置分享图片, 参数2为图片的url地址
mController.setShareMedia(new UMImage(context,
pic_url));
// 设置分享图片,参数2为本地图片的资源引用
//mController.setShareMedia(new UMImage(getActivity(), R.drawable.icon));
// 设置分享图片,参数2为本地图片的路径(绝对路径)
//mController.setShareMedia(new UMImage(getActivity(),
// BitmapFactory.decodeFile("/mnt/sdcard/icon.png")));
// 设置分享音乐
//UMusic uMusic = new UMusic("http://sns.whalecloud.com/test_music.mp3");
//uMusic.setAuthor("GuGu");
//uMusic.setTitle("天籁之音");
// 设置音乐缩略图
//uMusic.setThumb("http://www.umeng.com/images/pic/banner_module_social.png");
//mController.setShareMedia(uMusic);
// 设置分享视频
//UMVideo umVideo = new UMVideo(
// "http://v.youku.com/v_show/id_XNTE5ODAwMDM2.html?f=19001023");
// 设置视频缩略图
//umVideo.setThumb("http://www.umeng.com/images/pic/banner_module_social.png");
//umVideo.setTitle("友盟社会化分享!");
//mController.setShareMedia(umVideo);
}
public static void qqShare(Context context,UMSocialService mController,String content,String title,String pic_url){
//参数1为当前Activity, 参数2为开发者在QQ互联申请的APP ID,
//参数3为开发者在QQ互联申请的APP kEY.
UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity) context, "100424468",
"c7394704798a158208a74ab60104f0ba");
qqSsoHandler.addToSocialSDK();
QQShareContent qqShareContent = new QQShareContent();
qqShareContent.setShareContent(content);
qqShareContent.setTitle(title);
// qqShareContent.setShareImage(new UMImage(context,
// pic_url));
qqShareContent.setShareImage(new UMImage(context,
R.drawable.ic_launcher));
qqShareContent.setTargetUrl(pic_url);
mController.setShareMedia(qqShareContent);
//参数1为当前Activity, 参数2为开发者在QQ互联申请的APP ID,
//参数3为开发者在QQ互联申请的APP kEY.
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity) context, "1101985158",
"Tu43jAOOzqYVZ5md");
qZoneSsoHandler.addToSocialSDK();
QZoneShareContent qzone = new QZoneShareContent();
qzone.setShareContent(content);
qzone.setShareImage(new UMImage(context,
R.drawable.ic_launcher));
qzone.setTargetUrl(pic_url);
qzone.setTitle(title);
// qzone.setShareImage(urlImage);
mController.setShareMedia(qzone);
}
public static void renRenShare(Context context,UMSocialService mController,String content,String pic_url){
//添加人人网SSO授权功能
//APPID:201874
//API Key:28401c0964f04a72a14c812d6132fcef
//Secret:3bf66e42db1e4fa9829b955cc300b737
RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler((Activity) context,
"201874", "28401c0964f04a72a14c812d6132fcef",
"3bf66e42db1e4fa9829b955cc300b737");
mController.getConfig().setSsoHandler(renrenSsoHandler);
RenrenShareContent renren=new RenrenShareContent();
renren.setShareContent(content);
renren.setShareImage(new UMImage(context, "http://c.hiphotos.baidu.com/image/w%3D310/sign=96d6166f97eef01f4d141ec4d0ff99e0/7e3e6709c93d70cf199e0506fbdcd100baa12b50.jpg"));
renren.setTargetUrl(pic_url);
renren.setTitle("");
mController.setShareMedia(renren);
}
}
调用的时候会出现人人和其他的不能同时出现的问题,只需要在代码里加上这样一行代码来设置所要分享的平台
mController.getConfig().setPlatforms(SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,SHARE_MEDIA.RENREN,SHARE_MEDIA.TENCENT);
最后就只有调用出发显示的方法了
我的方法体是这样的
mController.getConfig().removePlatform( SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
// 是否只有已登录用户才能打开分享选择页
mController.openShare(MainActivity.this, false);
ShareDemos.weixinShareDemo(MainActivity.this, mController, appID, shareString, null, path_share);
ShareDemos.conmanShare(MainActivity.this, mController,shareString,null,path_share);
ShareDemos.qqShare(MainActivity.this, mController,shareString,null,path_share);
ShareDemos.renRenShare(MainActivity.this, mController,shareString,path_share);
现在可以运行看效果了