iOS大咖说布吉岛分享到其他应用

ShareSDK分享到指定平台攻略

2017-09-20  本文已影响286人  布吉岛原住民

本文介绍的是调用ShareSDK的内置方法实现分享到指定平台的功能,想了解ShareSDK的集成以及如何实现快捷分享,请参考官方文档。

分享到QQ
public static void showShare() {
ShareSDK.initSDK(this);

OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权

oks.setTitle(“标题”);
oks.setTitleUrl(“http://www.baidu.com”);
oks.setText(“text”);
oks.setImageUrl(“http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg”);
oks.setPlatform(QQ.NAME);
oks.show(this);
}
public static void showShare() {
ShareSDK.initSDK(this);
QQ.ShareParams sp = new QQ.ShareParams();

sp.setTitle("标题");
sp.setTitleUrl("http://www.baidu.com"); 
sp.setText("text");
sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
Platform qq = ShareSDK.getPlatform(QQ.NAME);
qq.share(sp);
}
分享到QQ空间
public static void showShare() {
ShareSDK.initSDK(this);
OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权

oks.setTitle("标题");
oks.setTitleUrl("http://www.baidu.com");
oks.setText("text");
oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
oks.setSite("发布分享的网站名称");//QQ空间的字段,标记分享应用的名称
oks.setSiteUrl("发布分享网站的地址");//QQ空间的字段,标记分享应用的网页地址
oks.setPlatform(QZone.NAME);
oks.show(this);
}
public static void showShare() {
ShareSDK.initSDK(this);
QZone.ShareParams sp = new QZone.ShareParams();

sp.setTitle("标题");
sp.setTitleUrl("http://www.baidu.com");
sp.setText("text");
sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
sp.setSite("发布分享的网站名称");//QQ空间的字段,标记分享应用的名称
sp.setSiteUrl("发布分享网站的地址");//QQ空间的字段,标记分享应用的网页地址
Platform qZone =ShareSDK.getPlatform(QZone.NAME);
qZone.share(sp);
}
分享到新浪微博
public static void showShare() {
ShareSDK.initSDK(this);
OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权

oks.setText("text" + "http://www.baidu.com");
oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
oks.setPlatform(SinaWeibo.NAME);
oks.show(this);
}
public static void showShare() {
ShareSDK.initSDK(this);
SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();

sp.setText("text" + "http://www.baidu.com");//没有title,文本和链接写在一起
sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
Platform weibo=ShareSDK.getPlatform(SinaWeibo.NAME);
weibo.share(sp);
}
分享给微信好友
public static void showShare() {
ShareSDK.initSDK(this);
OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权

oks.setTitle("标题");
oks.setUrl("http://www.baidu.com");
oks.setText("text");
oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
oks.setPlatform(Wechat.NAME);
oks.show(this);
}
public static void showShare() {
ShareSDK.initSDK(this);
Wechat.ShareParams sp = new Wechat.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); 

sp.setTitle("标题");
sp.setUrl("http://www.baidu.com");
sp.setText("text");
sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
Platform weixin = ShareSDK.getPlatform(Wechat.NAME); 
weixin.share(sp);
}
分享到微信朋友圈
public static void showShare() {
ShareSDK.initSDK(this);
OnekeyShare oks = new OnekeyShare();
oks.disableSSOWhenAuthorize();//关闭sso授权

oks.setTitle("标题");
oks.setUrl("http://www.baidu.com");
oks.setText("text");
oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
oks.setPlatform(WechatMoments.NAME);
oks.show(this);
}
public static void showShare() {
ShareSDK.initSDK(this);
WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE);

sp.setTitle("标题");
sp.setUrl("http://www.baidu.com");
sp.setText("text");
sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
Platform weixinGroup = ShareSDK.getPlatform(WechatMoments.NAME);
weixinGroup.share(sp);
}
注意事项:

OneKeyShare本身已经做了分享回调处理,而使用ShareParams实现分享的时候需要自己另外处理,监听代码如下(这里以QQ空间分享为例):

// 设置分享事件回调(注:回调不能保证在主线程调用,不可以在里面直接处理UI操作)
Platform qzone = ShareSDK.getPlatform (QZone.NAME);
qzone.setPlatformActionListener (new PlatformActionListener() {
    public void onError(Platform arg0, int arg1, Throwable arg2) {
        //失败的回调,arg:平台对象,arg1:表示当前的动作,arg2:异常信息
    }
    public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
        //分享成功的回调
    }
    public void onCancel(Platform arg0, int arg1) {
        //取消分享的回调
    }
});
上一篇 下一篇

猜你喜欢

热点阅读