ShareSdk分享工具类

来源:互联网 发布:淘宝在台湾 编辑:程序博客网 时间:2024/05/17 18:01
package com.yuezhi.zaixianjiaoyu.onekeyshare;import android.content.Context;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.text.Html;import android.text.TextUtils;import com.yuezhi.zaixianjiaoyu.R;import java.util.List;import java.util.Locale;import java.util.Map;import cn.sharesdk.framework.Platform;import cn.sharesdk.framework.PlatformActionListener;import cn.sharesdk.framework.ShareSDK;import cn.sharesdk.tencent.qq.QQ;import cn.sharesdk.tencent.qzone.QZone;import cn.sharesdk.wechat.friends.Wechat;import cn.sharesdk.wechat.moments.WechatMoments;/** * Created by zhm on 2017/12/12. * <p> * <p> * 自定义  Shared   Utils */public class CustomShareUtils {    //单独分享指定到QQ/QQ空间分享平台    public static void singleQQShare(String title, String titleUrl, String txt, String imgUrl, String site, String siteUrl, PlatformActionListener platformActionListener) {        Platform.ShareParams sp = new Platform.ShareParams();        sp.setTitle(title);        sp.setTitleUrl(titleUrl);// 标题的超链接        sp.setText(txt);        sp.setImageUrl(imgUrl);        sp.setSite(site);        sp.setSiteUrl(siteUrl);        Platform qq = ShareSDK.getPlatform(QQ.NAME);// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)        qq.setPlatformActionListener(platformActionListener);        // 执行图文分享        qq.share(sp);    }    public static void singleQZShare(String title, String titleurl, String text, String imgUrl, String site, String siteUrl, PlatformActionListener platformActionListener) {        Platform.ShareParams sp = new Platform.ShareParams();        sp.setTitle(title);        sp.setTitleUrl(titleurl);// 标题的超链接        sp.setText(text);        sp.setImageUrl(imgUrl);        sp.setSite(site);        sp.setSiteUrl(siteUrl);        Platform qzone = ShareSDK.getPlatform(QZone.NAME);// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)        qzone.setPlatformActionListener(platformActionListener);// 执行图文分享        qzone.share(sp);    }    public static void singleWXShare(String title, String text, String url, String imgurl, PlatformActionListener platformActionListener) {        Platform.ShareParams sp = new Platform.ShareParams();        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性//----------仅限制微信否则分享只是文字        sp.setTitle(title);        sp.setUrl(url);        sp.setImageUrl(imgurl);        if (!TextUtils.isEmpty(text)) {            sp.setText(text);        }        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)        wechat.setPlatformActionListener(platformActionListener);// 执行图文分享        wechat.share(sp);    }    public static void singleWXMShare(String title, String url, String text, String imgurl, PlatformActionListener platformActionListener) {        Platform.ShareParams sp = new Platform.ShareParams();        sp.setTitle(title);        sp.setUrl(url);        sp.setImageUrl(imgurl);        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性//----------仅限制微信否则分享只是文字        if (!TextUtils.isEmpty(text)) {            sp.setText(text);//----------微信分享如果setText 为空 也有可能掉不起来        }        Platform wechat = ShareSDK.getPlatform(WechatMoments.NAME);// 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)        wechat.setPlatformActionListener(platformActionListener);// 执行图文分享        wechat.share(sp);    }    /**     * 判断是否安装了微博     *     * @param context     * @return     */    public static boolean isWeiboInstalled(Context context) {        final PackageManager packageManager = context.getPackageManager();        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);        if (pinfo != null) {            for (int i = 0; i < pinfo.size(); i++) {                String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);                if (pn.equals("com.sina.weibo")) {                    return true;                }            }        }        return false;    }    /**     * 判断是否安装了微信     *     * @param context     * @return     */    public static boolean isWeixinInstalled(Context context) {        final PackageManager packageManager = context.getPackageManager();        // 获取所有已安装程序的包信息        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);        if (pinfo != null) {            for (int i = 0; i < pinfo.size(); i++) {                String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);                if (pn.equals("com.tencent.mm")) {                    return true;                }            }        }        return false;    }    /**     * 判断是否安装了QQ     *     * @param context     * @return     */    public static boolean isQQClientInstalled(Context context) {        final PackageManager packageManager = context.getPackageManager();        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);        if (pinfo != null) {            for (int i = 0; i < pinfo.size(); i++) {                String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);                if (pn.equals("com.tencent.mobileqq")) {                    return true;                }            }        }        return false;    }    /*  //关闭sso授权    oks.disableSSOWhenAuthorize();    // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法    //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));    // title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用    oks.setTitle(title);    // titleUrl是标题的网络链接,仅在人人网和QQ空间使用    oks.setTitleUrl("http://www.jnjyzy.com/course/" + courseid + "/reviews");    // text是分享文本,所有平台都需要这个字段    oks.setText(String.valueOf(Html.fromHtml(about)));    // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数    oks.setImagePath(largePicture);//确保SDcard下面存在此张图片*//*        // url仅在微信(包括好友和朋友圈)中使用        oks.setUrl("http://www.jnjyzy.com/course/" + courseid + "/reviews");        // comment是我对这条分享的评论,仅在人人网和QQ空间使用*//*        oks.setComment("我是测试评论文本");*//*        // site是分享此内容的网站名称,仅在QQ空间使用        oks.setSite(getString(R.string.app_name));        // siteUrl是分享此内容的网站地址,仅在QQ空间使用        oks.setSiteUrl("http://www.jnjyzy.com/course/" + courseid + "/reviews");        // 启动分享GUI        oks.show(this);*/    //一键分享    public static void showShare(Context context, Map<String, String> map) {        OnekeyShare oks = new OnekeyShare();        oks.setShareContentCustomizeCallback(new ShareContentCustomizeDemo(context, map));        oks.show(context);    }    static class ShareContentCustomizeDemo implements ShareContentCustomizeCallback {        private Map<String, String> map;        private String pubTitle, pubText, url;        private Context c;        public ShareContentCustomizeDemo(Context c, Map<String, String> map) {            this.map = map;            this.c = c;            pubTitle = this.map.get("title");  ///公共的  写在这            pubText = this.map.get("text");            url = this.map.get("url");        }        public void onShare(Platform platform, Platform.ShareParams paramsToShare) {            // 改写twitter分享内容中的text字段,否则会超长,            // 因为twitter会将图片地址当作文本的一部分去计算长度            if (QQ.NAME.equals(platform.getName())) {                if(!TextUtils.isEmpty(pubTitle)){                    paramsToShare.setTitle(pubTitle);                }else{                    paramsToShare.setTitle("");                }                if(!TextUtils.isEmpty(url)){                    paramsToShare.setTitleUrl(url);                }                // text是分享文本,所有平台都需要这个字段                if (!TextUtils.isEmpty(pubText)) {                    paramsToShare.setText(String.valueOf(Html.fromHtml(pubText)));                } else {                    paramsToShare.setText("");//如果这个为空有可能调qq掉不起来                }                if(!TextUtils.isEmpty(map.get("pic"))){                    paramsToShare.setImagePath(map.get("pic"));                }            } else if (QZone.NAME.equals(platform.getName())) {                paramsToShare.setTitle(pubTitle);                paramsToShare.setTitleUrl(url);                // text是分享文本,所有平台都需要这个字段                paramsToShare.setSiteUrl(url);                paramsToShare.setSite(c.getString(R.string.app_name));                if(!TextUtils.isEmpty(map.get("pic"))){                    paramsToShare.setImagePath(map.get("pic"));                }                if (!TextUtils.isEmpty(pubText)) {                    paramsToShare.setText(String.valueOf(Html.fromHtml(pubText)));                } else {                    paramsToShare.setText("");//如果这个为空调用qq掉不起来                }            } else if (Wechat.NAME.equals(platform.getName())) {                paramsToShare.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性                paramsToShare.setTitle(pubTitle);                // text是分享文本,所有平台都需要这个字段                paramsToShare.setUrl(url);                if(!TextUtils.isEmpty(map.get("pic"))){                    paramsToShare.setImagePath(map.get("pic"));                }                if (!TextUtils.isEmpty(pubText)) {                    paramsToShare.setText(String.valueOf(Html.fromHtml(pubText)));                } else {                    paramsToShare.setText("");//如果这个为空调用qq掉不起来                }            } else if (WechatMoments.NAME.equals(platform.getName())) {                paramsToShare.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性                paramsToShare.setTitle(pubTitle);                // text是分享文本,所有平台都需要这个字段                paramsToShare.setUrl(url);                if(!TextUtils.isEmpty(map.get("pic"))){                    paramsToShare.setImagePath(map.get("pic"));                }                if (!TextUtils.isEmpty(pubText)) {                    paramsToShare.setText(String.valueOf(Html.fromHtml(pubText)));                } else {                    paramsToShare.setText("");//如果这个为空调用qq掉不起来                }            }        }    }}

paramsToShare.setShareType(Platform.SHARE_WEBPAGE);和微信相关的这个一定要设置否则分享出去的只能是文字

原创粉丝点击