前一段时间项目中使用到了友盟的一些功能,自己做了一个小demo,已经上传,今天有空,给大家讲解一下吧。
首先下载最新的友盟sdkhttp://dev.umeng.com/social/android/sdk-download,友盟一共给了三种方法,嵌入自己的工程,建议使用第一种,简单粗暴,直接导入。
http://dev.umeng.com/social/android/quick-integration 详情见友盟的api,我就不罗嗦了。
然后看代码
这是一个获取用户信息,并且使用xutils 上传服务器。
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
// UMSsoHandler ssoHandler;
private ImageView mImageView;
// private String url =
// "http://www.5imaxbeauty.com/appapi/app_weixin_data_uplad.php";
private String url = "http://192.168.1.155:81/app_api/app_weixin_data_uplad.php";
private String weiboUrl = "http://www.5imaxbeauty.com/appapi/app_weibo_data_uplad.php";
UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.umeng_example_socialize_customplatform_example);
// 初始化控件
initViews();
// 定义各个平台
configPlatforms();
// 设置分享内容
setShareContent();
// 推送服务启动
pushMessage();
}
private void pushMessage() {
PushAgent mPushAgent = PushAgent.getInstance(this);
mPushAgent.enable();
}
private void initViews() {
mImageView = (ImageView) findViewById(R.id.scrshot_imgview);
// 新浪登录
findViewById(R.id.login_sina).setOnClickListener(this);
// 微信登录
findViewById(R.id.login_qq).setOnClickListener(this);
// 分享
findViewById(R.id.shake_openshare).setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.login_sina:
login(SHARE_MEDIA.SINA, 1);
break;
case R.id.login_qq:
login(SHARE_MEDIA.WEIXIN, 2);
break;
case R.id.shake_openshare:
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA);
mController.openShare(this, false);
break;
}
}
/**
* 根据平台登录
*/
private void login(final SHARE_MEDIA platform, final int id) {
mController.doOauthVerify(this, platform, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA platform) {
Log.e(TAG, "授权开始");
}
@Override
public void onError(SocializeException e, SHARE_MEDIA platform) {
Log.e(TAG, "错误");
}
@Override
public void onComplete(Bundle value, SHARE_MEDIA platform) {
String uid = value.getString("uid");
if (!TextUtils.isEmpty(uid)) {
getUserInfo(platform, id);
} else {
Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Log.e(TAG, "取消");
}
});
}
/**
* 获取用户信息
*/
private void getUserInfo(SHARE_MEDIA platform, final int id) {
mController.getPlatformInfo(this, platform, new UMDataListener() {
private Gson gson;
@Override
public void onStart() {
}
@Override
public void onComplete(int status, Map
if (info != null) {
gson = new Gson();
String json = gson.toJson(info);
Log.e(TAG, json);
if (id == 1) {
SinaInfo sinaInfo = gson.fromJson(json, SinaInfo.class);
upInfo(sinaInfo);
} else if (id == 2) {
WeixinInfo weixinInfo = gson.fromJson(json, WeixinInfo.class);
LogUtils.e("解析信息" + weixinInfo.toString());
upInfo(weixinInfo);
}
Toast.makeText(MainActivity.this, info.toString(), Toast.LENGTH_SHORT).show();
Log.e(TAG, info.toString());
// upDate(info.toString());
}
}
});
}
// uid,favourites_count,location,description,verified,friends_count,gender,screen_name,statuses_count,profile_image_url,
// unionid,nickname,sex,province,openid,headimgurl,country,city,update_time
public void upInfo(WeixinInfo weixinInfo) {
RequestParams params = new RequestParams();
params.addBodyParameter("unionid", weixinInfo.getUnionid());
params.addBodyParameter("nickname", weixinInfo.getNickname());
LogUtils.e(weixinInfo.toString() + "获取到的信息");
params.addBodyParameter("sex", weixinInfo.getSex());
params.addBodyParameter("province", weixinInfo.getProvince());
params.addBodyParameter("openid", weixinInfo.getOpenid());
params.addBodyParameter("headimgurl", weixinInfo.getHeadimgurl());
params.addBodyParameter("country", weixinInfo.getCountry());
params.addBodyParameter("city", weixinInfo.getCity());
// params.addBodyParameter("update_time", weixinInfo.getu);
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack
@Override
public void onFailure(HttpException arg0, String arg1) {
// Toast.makeText(mContext, "上传失败", 0).show();
Toast.makeText(MainActivity.this, "上传失败", 0).show();
LogUtils.e(arg0.toString() + arg1.toString());
}
@Override
public void onSuccess(ResponseInfo
Toast.makeText(MainActivity.this, "上传成功", 0).show();
LogUtils.e("上传成功");
}
});
}
// uid,favourites_count,location,description,verified,friends_count,gender,
// screen_name,statuses_count,followers_count,profile_image_url,
public void upInfo(SinaInfo sinaInfo) {
LogUtils.e(sinaInfo.toString());
RequestParams params = new RequestParams();
params.addBodyParameter("uid", sinaInfo.getUid());
params.addBodyParameter("favourites_count", sinaInfo.getFavourites_count());
// LogUtils.e(arg0.toString() + arg1.toString());
params.addBodyParameter("location", sinaInfo.getLocation());
params.addBodyParameter("description", sinaInfo.getDescription());
params.addBodyParameter("verified", sinaInfo.getVerified());
params.addBodyParameter("friends_count", sinaInfo.getFriends_count());
params.addBodyParameter("gender", sinaInfo.getGender());
params.addBodyParameter("screen_name", sinaInfo.getScreen_name());
params.addBodyParameter("statuses_count", sinaInfo.getStatuses_count());
params.addBodyParameter("followers_count", sinaInfo.getFollowers_count());
params.addBodyParameter("profile_image_url", sinaInfo.getProfile_image_url());
// params.addBodyParameter("update_time", weixinInfo.getu);
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.POST, weiboUrl, params, new RequestCallBack
@Override
public void onFailure(HttpException arg0, String arg1) {
// Toast.makeText(mContext, "上传失败", 0).show();
Toast.makeText(MainActivity.this, "上传失败", 0).show();
LogUtils.e(arg0.toString() + arg1.toString());
}
@Override
public void onSuccess(ResponseInfo
Toast.makeText(MainActivity.this, "新浪上传成功", 0).show();
}
});
}
/**
* 初始化各个平台
*/
private void configPlatforms() {
// 新浪sso
// mController.getConfig().setSsoHandler(new SinaSsoHandler());
// 添加微信好友朋友圈
addWXPlatform();
}
private void addWXPlatform() {
String appId = "wxea7f8e0eacc321bc";
String appSecret = "df572ccac09b85cf27d2d8a7b7acf1ec";
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(this, appId, appSecret);
wxHandler.addToSocialSDK();
// 添加朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(this, appId, appSecret);
wxCircleHandler.setToCircle(true);
wxCircleHandler.addToSocialSDK();
}
/**
* 设置分享内容
*/
private void setShareContent() {
UMImage localImage = new UMImage(this, R.drawable.ic_launcher);
UMImage urlImage = new UMImage(this, "http://www.umeng.com/images/pic/social/integrated_3.png");
// UMImage resImage = new UMImage(getActivity(), 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");
// UMEmoji emoji = new UMEmoji(getActivity(),
// "http://www.pc6.com/uploadimages/2010214917283624.gif");
// UMEmoji emoji = new UMEmoji(getActivity(),
// "/storage/sdcard0/emoji.gif");
WeiXinShareContent weixinContent = new WeiXinShareContent();
weixinContent.setShareContent("美爆了");
weixinContent.setTitle("美爆了");
weixinContent.setTargetUrl("http://www.umeng.com/social");
weixinContent.setShareMedia(urlImage);
mController.setShareMedia(weixinContent);
// 朋友圈内容
CircleShareContent circleMedia = new CircleShareContent();
circleMedia.setShareContent("美爆了");
circleMedia.setTitle("美爆了");
circleMedia.setShareMedia(urlImage);
// circleMedia.setShareMedia(uMusic);
// circleMedia.setShareMedia(video);
circleMedia.setTargetUrl("http://www.umeng.com/social");
mController.setShareMedia(circleMedia);
SinaShareContent sinaContent = new SinaShareContent();
sinaContent.setShareContent("美爆了");
sinaContent.setShareImage(urlImage);
sinaContent.setShareMedia(video);
mController.setShareMedia(sinaContent);
}
使用sso登录一定要加回调哦
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 回调接口
UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
Log.e(TAG, "方法回调");
if (ssoHandler != null) {
Log.e(TAG, "方法回调2");
ssoHandler.authorizeCallBack(requestCode, resultCode, data);
}
}
public void upDate(final String info) {
RequestParams params = new RequestParams();
HttpUtils httpUtils = new HttpUtils();
try {
params.setBodyEntity(new StringEntity(info, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpUtils.send(HttpMethod.POST, "http://www.5imaxbeauty.com/appapi/app_weixin_data_uplad.php", params,
new RequestCallBack
@Override
public void onFailure(HttpException arg0, String arg1) {
// Toast.makeText(this, "上传失败", 0).show();
LogUtils.e("上传失败");
}
@Override
public void onSuccess(ResponseInfo
LogUtils.e("上传成功" + arg0.toString());
Toast.makeText(MainActivity.this, "上传成功", 0).show();
}
});
}
}
最主要的是要去微博,微信后台去申请key,然后获取到自己应用签名md5 加密的值,填写到后台
这个是微博的,微信同理可得。。。。。。。
需要注意的是,微信的key 需要代码添加,如上述代码所示,而微博的则需要添加到友盟后台
如果你找不到这个界面,呵呵,那就别在it 圈混了,哈哈哈
还有一点容易遗漏的 就似~~~详情请见 http://bbs.umeng.com/thread-5394-1-1.html 因为微博大部分都被继承了,回调网址记得去后台改一下。OK就是这么多啦。
demo 下载地址 http://download.csdn.net/download/looookery/8593637,不谢
推送没什么东西,大家看demo吧。