关于ShareSDK第三方分享 —— 记录

从上班开始就一直忙着加班,项目上线,稍微清闲,整理一下之前三个来月的项目中使用的第三方分享

首先根据mob官方文档做好基本配置,包名签名要记得一致

关于ShareSDK第三方分享 —— 记录_第1张图片
导入官方的onekeyshare

ShareView是我们的自定义布局  一个GridLayout +一个view取消按钮

public class ShareView extends LinearLayout {

    private Context mContext;
    private GridLayout mGridLayout;
    private TextView cancle;
    private float paddingValue;

    private ArrayList entitys = new ArrayList<>();
    private ShareHelper shareHelper;

    public ShareView(Context context) {
        this(context, null);
    }

    public ShareView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        DisplayMetrics dm = getResources().getDisplayMetrics();

        paddingValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingValue, dm);

        this.mContext = context;

        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.share_dialog, this);
        mGridLayout = (GridLayout) findViewById(R.id.share_dialog_grid);
        cancle = (TextView) findViewById(R.id.share_dialog_cancle);
        setOrientation(VERTICAL);
    }

    /**
     * 设置分享子Item的点击事件 Method name: setShareItemClick 
* Description: setShareItemClick
*/ public void setShareResult(final ShareResult shareResult) { shareHelper = new ShareHelper(mContext, shareResult); mGridLayout.setGridLayoutItemListener(new GridLayout.GridLayoutItemListener() { @Override public boolean onItemLongClick(View view, int position) { return false; } @Override public void onItemClick(View view, int position) { ShareItemEntity entity = (ShareItemEntity) view.getTag(); if (null != entity) { switch (entity.getMethod()) { case weixin: shareHelper.sendToWx(); break; case weixin_circle: shareHelper.sendWxCircle(); break; case qq: shareHelper.sendQQ(); break; case qzone: shareHelper.sendQQZone(); break; case sina_weibo: shareHelper.sendSina(); break; case more: shareResult.onShareMore(); ToastUtil.show("链接已复制"); break; default: break; } } } }); } public void setCancleBtnOnClick(OnClickListener clickListener) { cancle.setOnClickListener(clickListener); } public TextView getCancle() { return cancle; } /** * 添加微信 Method name: addShareWx
* Description: addShareWx
* void
*/ public void addShareWx() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("微信"); entity.setImgRes(R.drawable.icon_share_wx); entity.setPlatform(ShareSDK.getPlatform(ShareHelper.Wechat)); entity.setMethod(ShareHelper.SHARE_METHOD.weixin); mGridLayout.addView(getShareItem(entity)); } /** * 添加新浪微博 Method name: addShareSina
* Description: addShareSina
* void
*/ public void addShareSina() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("新浪微博"); entity.setImgRes(R.drawable.icon_share_sina); entity.setPlatform(ShareSDK.getPlatform(ShareHelper.SinaWeibo)); entity.setMethod(ShareHelper.SHARE_METHOD.sina_weibo); mGridLayout.addView(getShareItem(entity)); } /** * 添加朋友圈 Method name: addShareWxCircle
* Description: addShareWxCircle
* void
*/ public void addShareWxCircle() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("朋友圈"); entity.setImgRes(R.drawable.icon_share_wechatmoents); entity.setPlatform(ShareSDK.getPlatform(ShareHelper.WechatMoments)); entity.setMethod(ShareHelper.SHARE_METHOD.weixin_circle); mGridLayout.addView(getShareItem(entity)); } /** * 添加QQ Method name: addShareQQ
* Description: addShareQQ
* void
*/ public void addShareQQ() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("QQ"); entity.setImgRes(R.drawable.icon_share_qq); entity.setPlatform(ShareSDK.getPlatform(ShareHelper.QQ)); entity.setMethod(ShareHelper.SHARE_METHOD.qq); mGridLayout.addView(getShareItem(entity)); } /** * 添加QQ空间 Method name: addShareQQZone
* Description: addShareQQZone
* void
*/ public void addShareQQZone() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("QQ空间"); entity.setImgRes(R.drawable.icon_share_qzone); entity.setPlatform(ShareSDK.getPlatform(ShareHelper.QQZone)); entity.setMethod(ShareHelper.SHARE_METHOD.qzone); mGridLayout.addView(getShareItem(entity)); } //复制链接 public void addShareCopy() { ShareItemEntity entity = new ShareItemEntity(); entity.setName("复制链接"); entity.setImgRes(R.drawable.icon_share_copy); entity.setMethod(ShareHelper.SHARE_METHOD.more); mGridLayout.addView(getShareItem(entity)); } /** * 得到子Item Method name: getShareItem
* Description: getShareItem
* * @param shareItemEntity * @return ShareItemView
*/ private ShareItemView getShareItem(ShareItemEntity shareItemEntity) { ShareItemView itemView = new ShareItemView(mContext); int width = (int) ((DeviceUtil.getScreenWidth() - paddingValue) / 4); // 估算每个item的宽 itemView.setData(shareItemEntity.getImgRes(), shareItemEntity.getName()); itemView.setViewWidth(new GridLayout.LayoutParams(), width); itemView.setTag(shareItemEntity); return itemView; } }



ShareItemView
public class ShareItemView extends LinearLayout {

    private Context context;
    private RelativeLayout item_rl;
    private ImageView item_iv;
    private TextView item_tv;

    public ShareItemView(Context context) {
        this(context, null);
    }

    public ShareItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShareItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        bindViews(context);
    }

    private void bindViews(Context context) {
        LayoutInflater.from(context).inflate(R.layout.share_item_view, this);
        item_rl = (RelativeLayout) findViewById(R.id.share_item_view_rl);
        item_iv = (ImageView) findViewById(R.id.share_item_view_iv);
        item_tv = (TextView) findViewById(R.id.share_item_view_tv);
        item_rl.setBackgroundResource(R.drawable.selector_default_click);
    }

    public ImageView getItem_plate__iv() {
        return item_iv;
    }

    /**
     * 设置数据,本地图片 Method name: setData 
* Description: setData
* * @param imgRes * @param name void
*/ public void setData(int imgRes, String name) { item_iv.setImageResource(imgRes); item_tv.setText(name); item_iv.setBackgroundResource(android.R.color.transparent); } public void setText(String text) { item_tv.setText(text); } /** * 设置ImageView的宽与高 Method name: setImageWidth
* Description: setImageWidth
* * @param width void
*/ public void setImageWidth(int width) { RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width); layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); layoutParams.topMargin = width / 4; item_iv.setLayoutParams(layoutParams); RelativeLayout.LayoutParams layoutParamsTv = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParamsTv.addRule(RelativeLayout.CENTER_HORIZONTAL); layoutParamsTv.addRule(RelativeLayout.BELOW, R.id.share_item_view_iv); layoutParamsTv.topMargin = width / 8; item_tv.setLayoutParams(layoutParamsTv); } /** * 设置整个View的宽高 Method name: setViewWidth
* Description: setViewWidth
* * @param layoutParams * @param width void
*/ public void setViewWidth(ViewGroup.LayoutParams layoutParams, int width) { layoutParams.width = width; layoutParams.height = width; setLayoutParams(layoutParams); setImageWidth(width / 2); } public void setData(ShareItemEntity itemEntity) { item_iv.setImageResource(itemEntity.getImgRes()); item_iv.setBackgroundResource(android.R.color.transparent); item_tv.setText(itemEntity.getPlatform().getName()); }
/**
 * 分享的弹框 
 */
public class ShareDialog extends BottomBaseDialog {

    private ShareResult mShareCallback;
    private Context mContext;
    private ShareView mShareView;

    public ShareDialog(Context context, View animateView) {
        super(context, animateView);
    }

    public ShareDialog(Context context, ShareResult shareCallback) {
        super(context);
        this.mContext = context;
        this.mShareCallback = shareCallback;
    }

    @Override
    public View onCreateView() {
        mShareView = new ShareView(mContext);
        setCancelable(true);
        return mShareView;
    }

    @Override
    public void setUiBeforShow() {
        mShareView.setBackgroundResource(android.R.color.white);
        mShareView.setCancleBtnOnClick(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mShareView.setShareResult(mShareCallback);
    }

    public ShareDialog addWx() {
        mShareView.addShareWx();
        return this;
    }

    public ShareDialog addWxCircle() {
        mShareView.addShareWxCircle();
        return this;
    }

    public ShareDialog addQQ() {
        mShareView.addShareQQ();
        return this;
    }

    public ShareDialog addQQZone() {
        mShareView.addShareQQZone();
        return this;
    }

    public ShareDialog addSina() {
        mShareView.addShareSina();
        return this;
    }

    public ShareDialog addShareCopy() {
        mShareView.addShareCopy();
        return this;
    }

    public ShareDialog addAll() {
        addWx();
        addWxCircle();
        addSina();
        addQQ();
        addQQZone();
        addShareCopy();
        return this;
    }
}

public class ShareHelper {

    public static String Wechat = "Wechat";
    public static String WechatMoments = "WechatMoments";
    public static String QQ = "QQ";
    public static String QQZone = "QZone";
    public static String SinaWeibo = "SinaWeibo";

    private Context mContext;
    private ShareResult shareResult;
    private PlatformActionListener platformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap hashMap) {
            if (null != shareResult) {
                shareResult.onShareSuccess(platform);
            }
            LogUtil.d(platform.getName() + "--" + i);
            ToastUtil.show("分享成功");
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            if (null != shareResult) {
                shareResult.onShareError(platform);
            }
            LogUtil.e(throwable.getMessage() + "--" + i);
            ToastUtil.show("分享失败");
        }

        @Override
        public void onCancel(Platform platform, int i) {
            if (null != shareResult) {
                shareResult.onShareCancel(platform);
            }

            LogUtil.e(platform.getName() + "--" + i);
        }
    };
    private ShareParam shareParam;

    public ShareHelper(Context context, ShareResult shareResult) {
        this.mContext = context;
        this.shareResult = shareResult;
    }
    //加参数是为了区分平台(我们的需求里是微博分享内容要加个url)
    private void initShareParam(Platform platform) {
        shareParam = shareResult.getShareParam(platform);
        if (null == shareParam) {
            shareParam = shareResult.getShareParam();
        }
    }

    public void sendQQ() {
        Platform platform = ShareSDK.getPlatform(QQ);
        initShareParam(platform);
        if (platform.isClientValid()) { //isClientValid() 判断有没有安装客户端,没有不显示或者提示安装
            QQ.ShareParams sp = new QQ.ShareParams();
            sp.setTitle(shareParam.getTitle());
            sp.setTitleUrl(shareParam.getTitleUrl()); // 标题的超链接
            sp.setText(shareParam.getText());
            if (shareParam.isImageLocal()) {
                sp.setImagePath(shareParam.getImageUrl());
            } else {
                sp.setImageUrl(shareParam.getImageUrl());
            }
            platform.setPlatformActionListener(platformActionListener); // 设置分享事件回调
            platform.share(sp);
        }else {
            ToastUtil.show(ResourceUtil.getString(R.string.ssdk_qq_client_inavailable));
        }
    }

    public void sendQQZone() {
        Platform platform = ShareSDK.getPlatform(QQZone);
        initShareParam(platform);
        if (platform.isClientValid()) {
            cn.sharesdk.tencent.qzone.QZone.ShareParams sp = new QZone.ShareParams();
            sp.setTitle(shareParam.getTitle());
            sp.setTitleUrl(shareParam.getTitleUrl()); // 标题的超链接
            sp.setText(shareParam.getText());
            if (shareParam.isImageLocal()) {
                sp.setImagePath(shareParam.getImageUrl());
            } else {
                sp.setImageUrl(shareParam.getImageUrl());
            }
            sp.setSite(shareParam.getSite());
            sp.setSiteUrl(shareParam.getSiteUrl());

            platform.setPlatformActionListener(platformActionListener); // 设置分享事件回调
            platform.share(sp);
        }else {
            ToastUtil.show(ResourceUtil.getString(R.string.ssdk_qq_client_inavailable));
        }
    }

    public void sendToWx() {
        Platform platform = ShareSDK.getPlatform(Wechat);
        initShareParam(platform);
        if (platform.isClientValid()) {
            final Wechat.ShareParams sp = new Wechat.ShareParams();
            sp.setShareType(Platform.SHARE_WEBPAGE);//微信分享网页要设置类型,微信单独在这边设置!!!!!!
            sp.setTitle(shareParam.getTitle());
            sp.setText(shareParam.getText());
            Glide.with(mContext).load(shareParam.getImageUrl()).asBitmap().into(new SimpleTarget() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    sp.setImageData(resource);
                }

            }); //方法中设置asBitmap可以设置回调类型
            sp.setUrl(shareParam.getToUrl());

            platform.setPlatformActionListener(platformActionListener); // 设置分享事件回调
            platform.share(sp);
        }else {
            ToastUtil.show(ResourceUtil.getString(R.string.ssdk_wechat_client_inavailable));
        }
    }

    public void sendWxCircle() {
        Platform platform = ShareSDK.getPlatform(WechatMoments);
        initShareParam(platform);
        if (platform.isClientValid()) {
            final cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
            sp.setShareType(Platform.SHARE_WEBPAGE);//微信分享网页要设置类型,微信单独在这边设置!!!!!!
            sp.setTitle(shareParam.getTitle());
            sp.setText(shareParam.getText());
            Glide.with(mContext).load(shareParam.getImageUrl()).asBitmap().into(new SimpleTarget() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    sp.setImageData(resource);
                }

            });
            sp.setUrl(shareParam.getToUrl());
            platform.setPlatformActionListener(platformActionListener); // 设置分享事件回调
            platform.share(sp);
        }else {
            ToastUtil.show(ResourceUtil.getString(R.string.ssdk_wechat_client_inavailable));
        }
    }

    public void sendSina() {
        Platform platform = ShareSDK.getPlatform(SinaWeibo);
        initShareParam(platform);
        if (platform.isClientValid()) {
            cn.sharesdk.sina.weibo.SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
            sp.setTitle(shareParam.getTitle());
            sp.setText(shareParam.getText());
            if (shareParam.isImageLocal()) {
                sp.setImagePath(shareParam.getImageUrl());
            } else {
                sp.setImageUrl(shareParam.getImageUrl());
            }
            platform.removeAccount(true);
            ShareSDK.removeCookieOnAuthorize(true);
            platform.SSOSetting(true);
            platform.setPlatformActionListener(platformActionListener); // 设置分享事件回调
            platform.share(sp);
        }else {
            ToastUtil.show(ResourceUtil.getString(R.string.ssdk_sina_client_inavailable));
        }
    }
    
    public enum SHARE_METHOD {
        weixin, weixin_circle, sina_weibo, tecent_weibo, sms, qq, qzone, more;
    }
}


/**
 * 分享回调
 */
public abstract class ShareResult {

    public abstract ShareParam getShareParam();

    public ShareParam getShareParam(Platform platform) {

        return null;
    }

    /**
     * 分享成功
     *
     * @param platform
     */
    public void onShareSuccess(Platform platform) {

    }

    /**
     * 分享失败
     *
     * @param platform
     */
    public void onShareError(Platform platform) {

    }

    /**
     * 取消分享
     *
     * @param platform
     */
    public void onShareCancel(Platform platform) {

    }

    /**
     * 复制
     */
    public void onShareMore() {

    }

}

分享图片,有时候会需要分享我们的logo,就分享本地图片

        //将drawable存到本地--网上找的,自己还不是很会,得记住啊
                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                String filename = MConfiguration.getImageCachePath() + "ic_launcher" + UUID.randomUUID().toString() + ".jpg";
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(filename);
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
                } catch (Exception e) {
                } finally {
                    try {
                        if (out != null)
                            out.flush();
                    } catch (IOException e) {
                    }
                    try {
                        if (out != null)
                            out.close();
                    } catch (IOException e) {
                    }
                    out = null;
                }
                ShareParam shareParam = new ShareParam();
                shareParam.setImageUrl(filename);//---放logo




总结一下我的问题:

最开始就不知道怎么回事就是分享不出去,事实上是AppKey和Appsecret  还有安卓包名签名的问题,大家配置要好好检查一下,
出现闪一下就是签名问题,或者没打正式包
微信分享不了:
1、测试没有打包(用keystroke打包apk)
2、打包的keystore跟微信开放平台上面的不一致, 导致MD5码不一致(微信开放平台填写的是MD5,ps: 填写到微信开放平台 然后把MD5中的 大写字母改成小写字母 冒号去掉)
3、分享参数错误(微信有分享类型的详情参考:http://wiki.mob.com/%e4%b8%8d%e5%90%8c%e5%b9%b3%e5%8f%b0%e5%88%86%e4%ba%ab%e5%86%85%e5%ae%b9%e7%9a%84%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e/)
4、应用没有审核通过
5、包名跟开放平台上面的不一致
6:没有在在mob.com申请key!
7:修改签名会在微信客户端有缓存的,要卸载重新安装微信客户端重新安装,微信开放平台修改签名有时候会延迟刷新,

微信分享文字图片  是绕过审核就能分享,但是要分享url就必须通过审核,就他BypassApproval="false" 

如果你下按照文档操作,选择分享平台,不小心选择多了,有你不想要的,直接删除libs下jar包就好 build.gradle里也删掉

新浪微博开发者平台---应用信息--高级信息--OAuth2.0授权设置,授权回调页 和我们ShareSDK.xml  RedirectUrl="https://api.weibo.com/oauth2/default.html"里要一致

如果有什么问题,最先就检查一下签名等配置,官方的没什么大问题,有也是我们操作有遗漏

http://blog.csdn.net/virgo826627/article/details/50996060

还有微博分享错误码,我遇到短时间分享相同的内容的错误(微博不允许短时间内重复分享 ),就是测试分享次数太多了,自己对应看下吧~~~   http://open.weibo.com/wiki/Error_code

如果,手机没装客户端,你判断提示用户安装,对,就是他   platform.isClientValid()找到判断一下
eg.   在Activity 里 判断微信平台      finalPlatform platform = ShareSDK.getPlatform(this, Wechat.NAME);

最后一个问题就是,分享到微信好友,shareParam.setTitle("要分享的标题");就几个字,会显示两遍,mob官方客服告诉,这个是微信控制的,我们改不了,那这个只能这样了


综上,是我遇到的所有问题的总结吧~~弄个分享,出来这么多问题,也是没谁了,菜鸟一枚,写出来,供大家参考




你可能感兴趣的:(记录)