ShareSDK自定义界面的分享及分享中注意问题整理

分享功能在app开发中算是一个常见功能,使用友盟的一键分享使用起来非常方便。但是实际项目可能会根据需求使用自定义界面的分享。在此做一个记录分享,也把遇到的问题整理出来。

前面的集成过程就不赘述了,根据官方文档一步步来就可以搞定。编写思路就是将功能封装在一个dialogfragment里面。在需要的地方直接调用就可以了。直接是上代码,大部分都有注释。效果如下图,界面大家可以随意编写,主要是介绍分享方法。
这里写图片描述

public class ShareFrag extends DialogFragment {

PlatformActionListener platformActionListener = new PlatformActionListener() {
    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        ShareFrag.this.getDialog().dismiss();
        //大部分的回调方法都处于网络线程,因此可以简单默认为回调方法都不在主线程.
        getActivity().runOnUiThread(new Runnable() {
            @Override
         public void run() {     
           Toast.makeText(getActivity(), "分享成功!",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.e("TAG", throwable.getMessage());
    }

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

private View rootView;
private ShareWebBean webBean;
private String url;
 private View.OnClickListener SHARE_LISTENER = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.layout_share_wechat: {
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE);//非常重要:一定要设置分享属性
                sp.setTitle(webBean.getCfg().getShareTitle()); //分享标题
                sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());   //分享文本
                sp.setImageUrl(webBean.getCfg().getShareIcon());
                sp.setUrl(url);   //网友点进链接后,可以看到分享的详情
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(platformActionListener); // 设置分享事件回调
                // 执行分享
                wechat.share(sp);
                break;
            }
            case R.id.layout_share_moments: {
                WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性

                sp.setTitle(webBean.getCfg().getShareTitle());  //分享标题
                sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());   //分享文本
                sp.setUrl(url);   //网友点进链接后,可以看到分享的详情
                sp.setImageUrl(webBean.getCfg().getShareIcon());
//

                //3、非常重要:获取平台对象
                Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                wechatMoments.setPlatformActionListener(platformActionListener); // 设置分享事件回调
                // 执行分享
                wechatMoments.share(sp);
                break;
            }
            case R.id.layout_share_weibo: {
                SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
                sp.setText(webBean.getCfg().getShareContent() + url); //分享文本
                sp.setTitle(webBean.getCfg().getShareTitle());
//                    sp.setImagePath(img);//本地图片rul
                //   sp.setImageUrl(imagePath);
                //3、非常重要:获取平台对象
                Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                sinaWeibo.setPlatformActionListener(platformActionListener); // 设置分享事件回调
                sinaWeibo.SSOSetting(false);//false是使用客户端授权
                // 执行分享
                sinaWeibo.share(sp);
                break;
            }
            case R.id.layout_share_qq: {
                QQ.ShareParams sp = new QQ.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
                sp.setTitle(webBean.getCfg().getShareTitle());
                sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());
                sp.setImageUrl(webBean.getCfg().getShareIcon());
                sp.setTitleUrl(url);  //网友点进链接后,可以看到分享的详情
                //3、非常重要:获取平台对象
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(platformActionListener); // 设置分享事件回调
                // 执行分享
                qq.share(sp);
                break;
            }
            case R.id.layout_share_zone: {
                QZone.ShareParams sp = new QZone.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
                sp.setTitle(webBean.getCfg().getShareTitle());
                sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());
                //              sp.setImagePath(urlWithHttp);
                sp.setImageUrl(webBean.getCfg().getShareIcon());
                sp.setTitleUrl(url);
                Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                qzone.setPlatformActionListener(platformActionListener); // 设置分享事件回调
                // 执行图文分享
                qzone.share(sp);
                break;
            }
            case R.id.layout_share_collect: {
                WechatFavorite.ShareParams sp = new WechatFavorite.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
                sp.setTitle(webBean.getCfg().getShareTitle());
                sp.setText(webBean.getCfg().getShareContent());
                //   sp.setImagePath(imagePath);
                sp.setImageUrl(webBean.getCfg().getShareIcon());
                sp.setUrl(url);
                Platform wechatFavorite = ShareSDK.getPlatform(WechatFavorite.NAME);
                wechatFavorite.setPlatformActionListener(platformActionListener);
                wechatFavorite.share(sp);
                break;
            }
        }
    }
};
public ShareFrag() {
    // Required empty public constructor

}
    @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (rootView == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        rootView = inflater.inflate(R.layout.fragment_share_dqg, null, false);
        RelativeLayout cancel = (RelativeLayout) rootView.findViewById(R.id.fragment_share_cancel);

        RelativeLayout layoutShareWechat = (RelativeLayout) rootView.findViewById(R.id.layout_share_wechat);
        RelativeLayout layoutShareQq = (RelativeLayout) rootView.findViewById(R.id.layout_share_qq);
        RelativeLayout layoutShareWeibo = (RelativeLayout) rootView.findViewById(R.id.layout_share_weibo);
        RelativeLayout layoutShareZone = (RelativeLayout) rootView.findViewById(R.id.layout_share_zone);
        RelativeLayout layoutShareMoments = (RelativeLayout)rootView.findViewById(R.id.layout_share_moments);
        RelativeLayout layoutShareCollect = (RelativeLayout)rootView.findViewById(R.id.layout_share_collect);

        layoutShareQq.setOnClickListener(SHARE_LISTENER);
        layoutShareWechat.setOnClickListener(SHARE_LISTENER);
        layoutShareWeibo.setOnClickListener(SHARE_LISTENER);
        layoutShareZone.setOnClickListener(SHARE_LISTENER);
        layoutShareMoments.setOnClickListener(SHARE_LISTENER);
        layoutShareCollect.setOnClickListener(SHARE_LISTENER);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
    ShareSDK.initSDK(activity);
}

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    Window window = dialog.getWindow();
    dialog.setCanceledOnTouchOutside(true);
    DisplayMetrics dm = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    window.setLayout(dm.widthPixels, ViewGroup.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.BOTTOM);
    window.setWindowAnimations(R.style.BottomDialogStyle);
    //设置背景透明,不设置dialog可能会出现黑边
    window.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.color.transparent));
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    return rootView;
}

@Override
public void onDestroy() {
    super.onDestroy();
    ((ViewGroup) (rootView.getParent())).removeView(rootView);
}

//设置数据,这个方法参数根据实际需要修改
public void setData(ShareWebBean shareWebBean,String uid) {
    this.webBean = shareWebBean;
//        myApplication = (qiantuhaoApplication) getActivity().getApplication();
    url = shareWebBean.getCfg().getShareUrl() + "?tjr=" + uid;
}
}
使用时候调用
    FragmentManager fm = getSupportFragmentManager();
    ShareFrag shareFrag = new ShareFrag();
    shareFrag.show(fm, null);
    shareFrag.setData(webBean,myApplication.getUid());//设置分享内容,这个方法可以根据各自需求传入不同数据进行处理。

问题整理:

微博:
  1. 分享网络图片要去微博开发者账号申请高级写入接口否则直接分享网络图片会报错。
  2. 分享直接调用微博客户端开始几次可能会出现失败,这个问题并不是我们问题,咨询友盟客服他们反映这个问题是微博那边问题这个bug存在了好几个版本。
  3. 分享网络图片时不能用https的链接,否则会爆出无法解析的错误,这个问题当时查了很久。。。其他分享图片最好也不要用https的链接。
qq空间:
  1. QQ空间要想使用qq客户端分享需要在ShareSDK.xml配置文件中将ShareByAppClient="true" 怎样直接调用QQ空间客户端进行分享官方文档里面好像没有说明。有知道的希望可以告知一下共同学习。
微信相关:
  1. 微信分享比较特殊的地方就是需要在开发者平台配置app的签名。在手机上下载好app,然后使用签名查看工具(微博等开发者平台上都有)就能知道app签名。
  2. 微信的相关分享需要安装微信客户端才行,测试时候要打出正式包运行在真机上。一般微信出现的问题都与签名有关系需要特别注意。
  3. 如果不想打正式包测试,可以在ShareSDK.xml配置文件中 BypassApproval="true"BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过审核的应用也可以执行分享,但是仅限于分享文字和图片,不能分享其他类型,默认值为false。此外,微信收藏不支持此字段。
  4. 在华为等一些android系统版本比较高的手机上,有可能出现调不起微信客户端的情况,咨询了客服以后。了解到微信本身是不支持网络图片分享的,我们在代码里设置的图片链接微信在调用的时候会去加载本地缓存的文件。在手机权限较高的情况下微信读取不到缓存的图片,就会发生调起失败的情况。解决办法就是直接写一个图片下载工具类。将网络图片下载到本地,直接加载本地图片。此方式可以解决大部分因为图片产生的分享失败的问题。

可能出问题的地方:

  1. 各个平台分享需要的字段有细微差别,有的缺少一个字段就会分享不成功或者无法调起分享界面。可以按照上面分享属性进行配置,而且分享的字段不要为空。
  2. sp.setImagePath(img);设置本地图片后再设置网络图片会以本地为准,网络图片会不生效。
  3. 分享回调信息可能不在主线程,不要在分享回调中进行处理UI的操作。
以后还有问题会继续补充。。。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值