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