ShareSDK Android 端分享与授权示例代码
作者:MobTech袤博科技
- 2022 年 9 月 08 日 上海
本文字数:4670 字
阅读完需:约 15 分钟
本文意在提供更便捷的分享和授权代码,仅供参考,希望可以给你带来更愉快的开发心情:
分享
分享分为两种方式:第一种通过 OneKeyShare 一键九宫格分享,第二种指定平台分享
一键九宫格分享多个平台
一键分享指的是通过 OneKeyShare 九宫格界面分享多个平台,不同平台分享不同的数据类型请参考:
OnekeyShare oks = new OnekeyShare();
oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
@Override
public void onShare(Platform platform, cn.sharesdk.framework.Platform.ShareParams paramsToShare) {
//微博分享链接和图文
if ("SinaWeibo".equals(platform.getName())) {
paramsToShare.setText("玩美夏日,护肤也要肆意玩酷!" + "www.mob.com");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
}
//微信好友分享网页
if ("Wechat".equals(platform.getName())) {
paramsToShare.setTitle("标题");
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
paramsToShare.setUrl("http://sharesdk.cn");
paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
Log.d("ShareSDK", paramsToShare.toMap().toString());
}
//微信朋友圈分享图片
if ("WechatMoments".equals(platform.getName())) {
paramsToShare.setTitle("标题");
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
/*Bitmap imageData = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
paramsToShare.setImageData(imageData);*/
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
paramsToShare.setShareType(Platform.SHARE_IMAGE);
Log.d("ShareSDK", paramsToShare.toMap().toString());
}
//QQ分享链接
if ("QQ".equals(platform.getName())) {
paramsToShare.setTitle("标题");
paramsToShare.setTitleUrl("http://sharesdk.cn");
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
Log.d("ShareSDK", paramsToShare.toMap().toString());
}
//支付宝好友分享网页
if ("Alipay".equals(platform.getName())) {
paramsToShare.setTitle("标题");
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
paramsToShare.setUrl("http://sharesdk.cn");
paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
Log.d("ShareSDK", paramsToShare.toMap().toString());
}
//Facebook以卡片形式分享链接
if ("Facebook".equals(platform.getName())) {
// paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
paramsToShare.setUrl("http://www.mob.com");
paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
paramsToShare.setQuote("我是共用的参数");
paramsToShare.setHashtag("测试话题分享");
}
//Twitter分享链接
if("Twitter".equals(platform.getName())){
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
paramsToShare.setUrl("http://sharesdk.cn");
}
//WhatsApp分享图片
if ("WhatsApp".equals(platform.getName())) {
paramsToShare.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
}
//短信分享文本
if("ShortMessage".equals(platform.getName())){
paramsToShare.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
paramsToShare.setTitle("标题");
paramsToShare.setAddress("17625325208");
}
}
});
oks.setCallback(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
Log.d("ShareLogin", "onComplete ----> 分享成功");
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.d("ShareLogin", "onError ----> 失败" + throwable.getStackTrace());
Log.d("ShareLogin", "onError ----> 失败" + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.d("ShareLogin", "onCancel ----> 分享取消");
}
});
// 启动分享GUI
oks.show(MobSDK.getContext());
复制代码
指定平台分享
这里列举下微信分享网页和 QQ 分享链接
微信分享网页
Wechat.ShareParams sp = new Wechat.ShareParams();
//微信分享网页的参数严格对照列表中微信分享网页的参数要求
sp.setTitle("标题");
sp.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
/*Bitmap imageData = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
sp.setImageData(imageData);*/
sp.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
sp.setUrl("http://sharesdk.cn");
sp.setShareType(Platform.SHARE_WEBPAGE);
Log.d("ShareSDK", sp.toMap().toString());
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)
wechat.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
Log.d("ShareSDK", "onComplete ----> 分享成功");
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.getStackTrace().toString());
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.d("ShareSDK", "onCancel ----> 分享取消");
}
});
// 执行图文分享
wechat.share(sp);
复制代码
QQ 分享链接
QQ.ShareParams sp = new QQ.ShareParams();
sp.setTitle("标题");
sp.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦");
sp.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
sp.setTitleUrl("http://sharesdk.cn");
Log.d("ShareSDK", sp.toMap().toString());
Platform qq = ShareSDK.getPlatform(QQ.NAME);
// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)
qq.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
Log.d("ShareSDK", "onComplete ----> 分享成功");
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.getStackTrace().toString());
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.d("ShareSDK", "onCancel ----> 分享取消");
}
});
// 执行图文分享
qq.share(sp);
复制代码
授权
授权代码比较固定,这里提供下微信授权、QQ 授权:
微信授权
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
//判定客户端是否可用
if (wechat.isClientValid()) {
//客户端可用
}
if (wechat.isAuthValid()) {
wechat.removeAccount(true);
}
wechat.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, final HashMap<String, Object> hashMap) {
/*platform.getDb().exportData()获取用户公共信息*/
Log.d("ShareSDK", "onComplete ----> 登录成功" + platform.getDb().exportData());
platform.getDb().getUserId();
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.toString());
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.getStackTrace().toString());
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.d("ShareSDK", "onCancel ----> 登录取消");
}
});
wechat.SSOSetting(false);
wechat.showUser(null);
复制代码
QQ 授权
Platform qq = ShareSDK.getPlatform(QQ.NAME);
//判定客户端是否可用
if (qq.isClientValid()) {
//客户端可用
}
if (qq.isAuthValid()) {
qq.removeAccount(true);
}
qq.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, final HashMap<String, Object> hashMap) {
/*platform.getDb().exportData()获取用户公共信息*/
Log.d("ShareSDK", "onComplete ----> 登录成功" + platform.getDb().exportData());
platform.getDb().getUserId();
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.toString());
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.getStackTrace().toString());
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.d("ShareSDK", "onCancel ----> 登录取消");
}
});
qq.SSOSetting(false);
qq.showUser(null);
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
MobTech袤博科技
关注
还未添加个人签名 2019.05.08 加入
还未添加个人简介
评论