本文意在提供更便捷的分享和授权代码,仅供参考,希望可以给你带来更愉快的开发心情:
分享
分享分为两种方式:第一种通过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 = https://www.it610.com/article/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 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());
指定平台分享 【ShareSDK Android端分享与授权示例代码】主流平台分享示例
这里列举下微信分享网页和QQ分享链接
- 微信分享网页
Wechat.ShareParams sp = new Wechat.ShareParams(); //微信分享网页的参数严格对照列表中微信分享网页的参数要求 sp.setTitle("标题"); sp.setText("我是共用的参数,这几个平台都有text参数要求,提取出来啦"); /*Bitmap imageData = https://www.it610.com/article/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 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 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 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 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);
推荐阅读
- Android分享---调用系统自带的分享功能
- 试用友盟SDK实现Android分享微信朋友圈
- 关于android分享(sharedsdk的简单使用)
- Android分享功能,微博QQQQ空间等社交平台分享之入门与进阶