App开发公共模块开发之社交分享组件

官网:http://www.mob.com/
我使用的是这种方式集成 :http://wiki.mob.com/sharesdk%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E9%9D%9Emaven/

第一步拷贝我们需要集成的jar包

image.png

拷贝我们需要的资源文件和微信入口文件

image.png

image.png

以上资源官网全部都提供了

mainfest中配置

  
        
            
            
                

                

                
                
            
        
        
        
      
        
        
        

简单使用

   public void testShareSDK() {
        //要分享对应平台的数据
        Platform.ShareParams params = new Platform.ShareParams();
        //我们要分享的平台
        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        //分享添加事件监听
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap hashMap) {

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

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

            }
        });
        //分享数据到platform平台
        platform.share(params);
    }

我们可以看到每次调用冗余都比较多,所以我们封装一下

ShareData:要分享的数据实体

public class ShareManager {

    private static ShareManager mShareManager = null;
    /**
     * 要分享到的平台
     */
    private Platform mCurrentPlatform;

    /**
     * 线程安全的单例模式
     */
    public static ShareManager getInstance() {
        if (mShareManager == null) {
            synchronized (ShareManager.class) {
                if (mShareManager == null) {
                    mShareManager = new ShareManager();
                }
            }
        }
        return mShareManager;
    }

    private ShareManager() {
    }

    //初始化我们的shareSDK组件
    public static void init(Context context) {
        MobSDK.init(context);
    }

    //分享数据入口
    public void shareData(ShareData data, PlatformActionListener listener) {
        switch (data.mType) {
            case QQ:
                mCurrentPlatform = ShareSDK.getPlatform(QQ.NAME);

                break;
            case QZONE:
                mCurrentPlatform = ShareSDK.getPlatform(QZone.NAME);
                break;
            case WeChat:
                mCurrentPlatform = ShareSDK.getPlatform(Wechat.NAME);
                break;
            case WeChatMoment:
                mCurrentPlatform = ShareSDK.getPlatform(WechatMoments.NAME);
                break;
            default:
                break;
        }
        mCurrentPlatform.setPlatformActionListener(listener);
        mCurrentPlatform.share(data.mParams);
    }

    //当前所支持的平台类型
    public enum PlatformType {
        QQ, QZONE, WeChat, WeChatMoment
    }
}

ShareDialog分享的dialog

public class ShareDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private DisplayMetrics dm;

    /**
     * 布局ui
     */
    private RelativeLayout mWeixinLayout;
    private RelativeLayout mWeixinMomentLayout;
    private RelativeLayout mQQLayout;
    private RelativeLayout mQZoneLayout;
    private RelativeLayout mDownloadLayout;
    private TextView mCancelView;

    /**
     * 当前支持的数据类型
     */
    private int mShareType; //指定分享类型
    private String mShareTitle; //指定分享内容标题
    private String mShareText; //指定分享内容文本
    private String mSharePhoto; //指定分享本地图片
    private String mShareTitleUrl;//分享图片的url
    private String mShareSiteUrl;
    private String mShareSite;
    private String mUrl;
    private String mResourceUrl;

    private boolean isShowDownload;

    public void setShareType(int shareType) {
        mShareType = shareType;
    }

    public void setShareTitle(String shareTitle) {
        mShareTitle = shareTitle;
    }

    public void setShareText(String shareText) {
        mShareText = shareText;
    }

    public void setSharePhoto(String sharePhoto) {
        mSharePhoto = sharePhoto;
    }

    public void setShareTitleUrl(String shareTitleUrl) {
        mShareTitleUrl = shareTitleUrl;
    }

    public void setShareSiteUrl(String shareSiteUrl) {
        mShareSiteUrl = shareSiteUrl;
    }

    public void setShareSite(String shareSite) {
        mShareSite = shareSite;
    }

    public void setUrl(String url) {
        mUrl = url;
    }

    public void setResourceUrl(String resourceUrl) {
        mResourceUrl = resourceUrl;
    }

    public ShareDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ShareDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        dm = mContext.getResources().getDisplayMetrics();
        this.isShowDownload = false;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share_layout);
        initView();
    }

    private void initView() {
        /**
         * 通过获取到dialog的window来控制dialog的宽高及位置
         */
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = dm.widthPixels; //设置宽度
        dialogWindow.setAttributes(lp);

        mWeixinLayout = (RelativeLayout) findViewById(R.id.weixin_layout);
        mWeixinLayout.setOnClickListener(this);
        mWeixinMomentLayout = (RelativeLayout) findViewById(R.id.moment_layout);
        mWeixinMomentLayout.setOnClickListener(this);
        mQQLayout = (RelativeLayout) findViewById(R.id.qq_layout);
        mQQLayout.setOnClickListener(this);
        mQZoneLayout = (RelativeLayout) findViewById(R.id.qzone_layout);
        mQZoneLayout.setOnClickListener(this);
        mDownloadLayout = (RelativeLayout) findViewById(R.id.download_layout);
        mDownloadLayout.setOnClickListener(this);
        if (isShowDownload) {
            mDownloadLayout.setVisibility(View.VISIBLE);
        }
        mCancelView = (TextView) findViewById(R.id.cancel_view);
        mCancelView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.weixin_layout:
                share(ShareManager.PlatformType.WeChat);
                break;
            case R.id.moment_layout:
                share(ShareManager.PlatformType.WeChatMoment);
                break;
            case R.id.qq_layout://分享到qq
                share(ShareManager.PlatformType.QQ);
                break;
            case R.id.qzone_layout:
                share(ShareManager.PlatformType.QZONE);
                break;
            case R.id.cancel_view:
                dismiss();
                break;

        }
    }


    //完成分享的方法
    private void share(ShareManager.PlatformType type) {
        //封装我们的shareData
        ShareData data = new ShareData();
        Platform.ShareParams params = new Platform.ShareParams();
        //设置参数
        params.setShareType(mShareType);
        params.setTitle(mShareTitle);
        params.setText(mShareText);
        params.setSite(mShareSite);
        params.setImagePath(mSharePhoto);
        params.setSiteUrl(mShareSiteUrl);
        params.setUrl(mUrl);
        data.mParams = params;
        data.mType = type;//qq 微信 qq空间  微信朋友圈
        ShareManager.getInstance().shareData(data, listener);
    }

    /**
     * 分享事件结果监听
     */
    private PlatformActionListener listener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap hashMap) {

        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
        }

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

        }
    };
}

效果如下


布局.png

你可能感兴趣的:(App开发公共模块开发之社交分享组件)