天天看點

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

繼續閱讀