本文意在提供更便捷的分享和受权代码,仅供参考,心愿能够给你带来更欢快的开发情绪:
分享
分享分为两种形式:第一种通过 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);