版权声明:本文为博主原创文章,未经博主允许不得转载。
说到第三方登录与分享,其实有很多sdk都支持,不过最常用的还算友盟和mob了,我今天打算记录一下sharesdk的第三方登录与分享,其实也就是简单实现,我只当,自己的个人笔记记录。也算梳理一下逻辑吧。
先看一下mob的官网。
我们要使用,就需要下载sdk,至于sdk的集成与环境配置,请你看,官方的:http://wiki.mob.com/Android_快速集成指南/ 它介绍的比什么都清楚。
具体我要提一下的是studio中的导入,因为我按着文档,也搞蒙了。
其实就是,对应的复制粘贴。
完成这些之后,再就是权限和activity的回调配置。
ShareSdk的第三方登录:
1.在activity中用代码进行初始化:
2.在点击事件中,写各种的实现方法:
/**
* 新浪微博第三方登录
*/
public static void sinaweiboLogin() {
//初始化新浪平台
Platform sinaPf = ShareSDK.getPlatform(context, SinaWeibo.NAME);
sinaPf.SSOSetting(true);//单点登录,设置为false,则在优先采用客户端授权,true会采用网页方式
//设置监听
sinaPf.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
Message msg = new Message();
msg.what = 10;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
@Override
public void onError(Platform platform, int action, Throwable throwable) {
Message msg = new Message();
msg.what = 10;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
@Override
public void onCancel(Platform platform, int action) {
Message msg = new Message();
msg.what = 10;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
});
//获取登录用户的信息,如果没有授权,会先授权,然后获取用户信息
sinaPf.authorize();
}
——————————————————————————————————————————————————————————————————————
/**
* 微信第三方登录
*/
private void weixinLogin() {
//初始化新浪平台
Platform weixinPf = ShareSDK.getPlatform(MobActivity.this, Wechat.NAME);
weixinPf.SSOSetting(false);//单点登录,设置为false,则在优先采用客户端授权,true会采用网页方式
//plat.isClientValid()判断是否有客户端
//设置监听
weixinPf.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
Message msg = new Message();
msg.what = 101;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
@Override
public void onError(Platform platform, int action, Throwable throwable) {
Message msg = new Message();
msg.what = 101;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = throwable;
handler.sendMessage(msg);
}
@Override
public void onCancel(Platform platform, int action) {
Message msg = new Message();
msg.what = 101;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
});
//获取登录用户的信息,如果没有授权,会先授权,然后获取用户信息
weixinPf.authorize();
}
——————————————————————————————————————————————————————————————————————————————
/**
* qq第三方登录
*/
private void qqLogin() {
//初始化qq平台
Platform qqPf = ShareSDK.getPlatform(MobActivity.this, QQ.NAME);
qqPf.SSOSetting(true);//单点登录
//设置监听
qqPf.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
Message msg = new Message();
msg.what = 100;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
@Override
public void onError(Platform platform, int action, Throwable throwable) {
Message msg = new Message();
msg.what = 100;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = throwable;
handler.sendMessage(msg);
}
@Override
public void onCancel(Platform platform, int action) {
Message msg = new Message();
msg.what = 100;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
handler.sendMessage(msg);
}
});
// qqPf.showUser(null);直接拿数据
qqPf.authorize();
}
3.对结果,进行处理。
————————————————————————————————————————————————————————————————————
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 10:
switch (msg.arg1) {
case 1:
//成功
Log.i("codelogin", "新浪登录:授权成功!");
//成功后的操作
Platform Pf = (Platform) msg.obj;
if (Pf != null) {
String id = Pf.getDb().getUserId();
String jsonData = Pf.getDb().exportData();
disposeResult(id, jsonData, "3");
}
break;
case 2:
//登录失败
Log.i("codelogin", "新浪登录:登录失败!");
break;
case 3:
//取消授权
Log.i("codelogin", "新浪登录:取消授权!");
break;
}
break;
case 100:
switch (msg.arg1) {
case 1:
//成功
Log.i("codelogin", "qq登录:授权成功!");
Platform QQPf = (Platform) msg.obj;
if (QQPf != null) {
String id = QQPf.getDb().getUserId();
String jsonData = QQPf.getDb().exportData();
disposeResult(id, jsonData, "1");
}
break;
case 2:
//登录失败
Log.i("codelogin", "qq登录:登录失败!");
break;
case 3:
//取消授权
Log.i("codelogin", "qq登录:取消授权!");
break;
}
break;
case 101:
switch (msg.arg1) {
case 1:
//成功
Log.i("codelogin", "微信登录:授权成功!");
Platform weixinPf = (Platform) msg.obj;
if (weixinPf != null) {
String id = weixinPf.getDb().getUserId();
String jsonData = weixinPf.getDb().exportData();
disposeResult(id, jsonData, "2");
}
break;
case 2:
//登录失败
Log.i("codelogin", "微信登录:登录失败!");
break;
case 3:
//取消授权
Log.i("codelogin", "微信登录:取消授权!");
break;
}
}
return false;
}
/**
* 处理第三方登录与后台接口
*
* @param id 第三方登录用户编码id
* @param jsonUser 第三方登录获取的全部用户信息
* @param flag 登录类型 1为QQ,2为微信 3为新浪微博
*/
public void disposeResult(String id, String jsonUser, String flag) {
NetWork.createApi(CodeLoginApi.class).thirdPartyLogin(NetWork.getUrlPostFix(),
new CodeLoginPostData().thirdPartyLogin(
"", id, flag, jsonUser,//手机号,第三方用户编码,登录类型,获取的第三方用户全部信息
new AMapLocationUtils().getMapLocationLng(context),//经度
new AMapLocationUtils().getMapLocationLat(context)//纬度
)).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<PageBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.i("codelogin", "第三方登录连接失败:" + e);
}
@Override
public void onNext(PageBean pageBean) {
Log.i("codelogin", "第三方登录连接成功:" + pageBean.ResultJson);
Toast.makeText(context, "" + pageBean.Message, Toast.LENGTH_SHORT).show();
}
});
}
shareSdk的第三方分享:
1.初始化
2.常规配置
3.进行分享设置 (微信和qq与其他分享是有不同的属性的)
4.启动分享
/**
* 第三方分享
*/
private void mobShare() {
//1.初始化,在oncreate中
//2.常规配置
OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权
//3.进行分享设置
oks.setText("三方分享");//分享标题
oks.setUrl("http://www.baidu.com");//分享网址
oks.setText("我是分享内容");
oks.setImageUrl("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
@Override
public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
Toast.makeText(MobActivity.this, "分享中", Toast.LENGTH_SHORT).show();
//可以放加载的Dialog
}
});
//4.启动分享
oks.show(this);
}
}