手动集成:(自动集成出了问题,所以就手动集成了)
去友盟开放平台下载友盟分享需要的SDK,把里面的jar包导入你的项目当中,jar包右键Add As Library即可
//分享的工具类
public class ShareUtils {
/**
* 分享链接
*/
public static void shareWeb(final Activity activity, String WebUrl, String title, String description, String imageUrl, int imageID, SHARE_MEDIA platform) {
UMWeb web = new UMWeb(WebUrl);//连接地址
web.setTitle(title);//标题
web.setDescription(description);//描述
if (TextUtils.isEmpty(imageUrl)) {
web.setThumb(new UMImage(activity, imageID)); //本地缩略图
} else {
web.setThumb(new UMImage(activity, imageUrl)); //网络缩略图
}
new ShareAction(activity)
.setPlatform(platform)
.withMedia(web)
.setCallback(new UMShareListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onResult(final SHARE_MEDIA share_media) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (share_media.name().equals("WEIXIN_FAVORITE")) {
Toast.makeText(activity, share_media + " 收藏成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(activity, share_media + " 分享成功", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onError(final SHARE_MEDIA share_media, final Throwable throwable) {
if (throwable != null) {
Log.d("throw", "throw:" + throwable.getMessage());
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, share_media + " 分享失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancel(final SHARE_MEDIA share_media) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, share_media + " 分享取消", Toast.LENGTH_SHORT).show();
}
});
}
})
.share();
//新浪微博中图文+链接
/*new ShareAction(activity)
.setPlatform(platform)
.withText(description + " " + WebUrl)
.withMedia(new UMImage(activity,imageID))
.share();*/
}
}
//分享的弹窗
public class ShareDialog extends Dialog {
private Context mContext;
private ImageView close;
private LinearLayout ll_wechat;
private LinearLayout ll_qq;
private LinearLayout ll_friend_circle;
public ShareDialog(@NonNull Context context) {
super(context);
mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(new BitmapDrawable());
setContentView(R.layout.dialog_share);
close = findViewById(R.id.close);
ll_wechat = findViewById(R.id.ll_wechat);
ll_qq = findViewById(R.id.ll_qq);
ll_friend_circle = findViewById(R.id.ll_friend_circle);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View pView) {
dismiss();
}
});
ll_wechat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View pView) {
if(mClick!=null){
mClick.clickWeChat();
dismiss();
}
}
});
ll_qq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View pView) {
if(mClick!=null){
mClick.clickQQ();
dismiss();
}
}
});
ll_friend_circle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View pView) {
if(mClick!=null){
mClick.clickFriendCircle();
dismiss();
}
}
});
}
Click mClick;
public void setClick(Click pClick) {
mClick = pClick;
}
public interface Click{
void clickWeChat();
void clickQQ();
void clickFriendCircle();
}
}
调用:
ShareDialog dialog = new ShareDialog(getActivity());
dialog.setClick(new ShareDialog.Click() {
@Override
public void clickWeChat() {
ShareUtils.shareWeb(mActivity, bean.getData().getShareUrl(), bean.getData().getTitle()
, bean.getData().getDescribes(), bean.getData().getHeadPortraitUrl()+"", 0, SHARE_MEDIA.WEIXIN);
}
@Override
public void clickQQ() {
ShareUtils.shareWeb(mActivity, bean.getData().getShareUrl(), bean.getData().getTitle()
, bean.getData().getDescribes(), bean.getData().getHeadPortraitUrl()+"", 0, SHARE_MEDIA.QQ);
}
@Override
public void clickFriendCircle() {
ShareUtils.shareWeb(mActivity, bean.getData().getShareUrl(), bean.getData().getTitle()
, bean.getData().getDescribes(), bean.getData().getHeadPortraitUrl()+"", 0, SHARE_MEDIA.WEIXIN_CIRCLE);
}
});
dialog.show();