shareSDK简单的用法

sharesdk 一键分享的用法,之前一直没整理,现在整理如下:

/**
 * Created by admin on 2016/6/22.
 */
public class ShareUtil {
    private Context context;
    private String title;
    private String titleUrl;
    private String text;
    private String imagePath;
    private String url;// 微博,朋友圈使用的url

    public ShareUtil() {
    }

    public ShareUtil(Context context) {
        this.context = context;
    }

    public ShareUtil(Context context, String title, String titleUrl, String text, String imagePath, String url) {
        this.context = context;
        this.title = title;
        this.titleUrl = titleUrl;
        this.text = text;
        this.imagePath = imagePath;
        this.url = url;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitleUrl() {
        return titleUrl;
    }

    public void setTitleUrl(String titleUrl) {
        this.titleUrl = titleUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    /**
     * showShare
     */
    public void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeDemo());
        oks.setCallback(new OneKeyShareCallback());
        oks.show(context);
    }


    /**
     * 1.新浪微博 分享图文	text	imagePath 注:微博分享链接是将链接写到setText内:eg:setText(“分享文本 http://baidu.com”);
     * 2&3.朋友圈分享网页	shareType(Platform.SHARE_WEBPAGE)	title	text(朋友圈不显示此字段)	imagePath	 url
     * 4.qq分享链接	title	titleUrl	text	imagePath
     * 5.email address	title	text
     */
    public class ShareContentCustomizeDemo implements ShareContentCustomizeCallback {

        public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
            String platformName = platform.getName();
            if (SinaWeibo.NAME.equals(platformName)) {
                // 新浪微博
                StringBuffer sb = new StringBuffer(text);
                paramsToShare.setText(sb.append(url).toString());
                paramsToShare.setImagePath(imagePath);
            } else if (Wechat.NAME.equals(platformName) || WechatMoments.NAME.equals(platformName)) {
                // 微信 & 朋友圈
                paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                paramsToShare.setTitle(title);
                paramsToShare.setText(text);
                paramsToShare.setImagePath(imagePath);
                paramsToShare.setUrl(url);
            } else if (QQ.NAME.equals(platformName)) {
                // QQ客户端
                paramsToShare.setTitle(title);
                paramsToShare.setTitleUrl(titleUrl);
                paramsToShare.setText(text);
                paramsToShare.setImagePath(imagePath);
            } else if (Email.NAME.equals(platformName)) {
                // email
                paramsToShare.setTitle(title);
                paramsToShare.setText(text);
            } else if (ShortMessage.NAME.equals(platformName)) {
                // 短信
                paramsToShare.setTitle(title);
                paramsToShare.setText(text);
            }
        }
    }

    /**
     * callback
     */
    public class OneKeyShareCallback implements PlatformActionListener {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(Platform platform, int i) {

        }
    }
}
另外增加标题栏和取消按钮等操作的具体地址,里面都有详细说明,更多需要的东西都可以查阅官方的文档等
http://bbs.mob.com/thread-21313-1-1.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值