Android 微信、qq分享弹框

1、自定义dialog

package cn.zhongshop.xdz.Utils.ShareManage;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;

import cn.zhongshop.xdz.Base.UtilsManage;
import com.zhongshop.xdz.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by Lxx on 2018/11/30.
 */

public class ShareDialog implements View.OnClickListener {
    private static final String TAG="ShareDialog";
    private static Activity activitys;
    private Dialog dialog;

    private UtilsManage utilsManage;

    public ShareDialog(Activity activity, UtilsManage manage, resultShareCallBack shareUtils) {
        activitys = activity;
        utilsManage = manage;
        resultShareCallBack = shareUtils;
    }

    private resultShareCallBack resultShareCallBack;

    public interface resultShareCallBack {
        void resultShare(String tv);
    }

    public void showdialog() {

        View localView = LayoutInflater.from(activitys).inflate(R.layout.layout_dialog_share, null);
        dialog = utilsManage.dialog(activitys, localView, Gravity.BOTTOM, false);

        localView.findViewById(R.id.share_qq).setOnClickListener(this);
        localView.findViewById(R.id.share_wx).setOnClickListener(this);
        localView.findViewById(R.id.share_pyq).setOnClickListener(this);
        localView.findViewById(R.id.share_wb).setOnClickListener(this);
        localView.findViewById(R.id.dialog_cencle).setOnClickListener(this);

    }
    private String urll,imgs,storename,goodsName;
    public void setUrl(String urll,String imgs,String storename,String goodsName){
        this.urll= urll;
        this.imgs= imgs;
        this.storename= storename;
        this.goodsName= goodsName;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_qq:
                dialog.dismiss();
                shareType(SHARE_MEDIA.QQ);
                break;
            case R.id.share_wx:
                dialog.dismiss();
                shareType(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.share_pyq:
                dialog.dismiss();
                shareType(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.share_wb:
                dialog.dismiss();
                shareType(SHARE_MEDIA.SINA);
                break;
            case R.id.dialog_cencle:
                dialog.dismiss();
                break;
        }
    }

    private void shareType(SHARE_MEDIA platform) {
        shareWeb(activitys, urll, storename
                , goodsName, imgs, R.mipmap.app_logo, platform
        );
    }

    /**
     * 分享链接
     */
    private void shareWeb(final Activity activity, String WebUrl, String title, String description,
                          String imageUrl, int imageID, SHARE_MEDIA platform) {

        Log.e(TAG, "shareWeb imageUrl: "+imageUrl );
        Log.e(TAG, "shareWeb imageID: "+imageID );
        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) {
                        resultShareCallBack.resultShare("分享成功");
                    }

                    @Override
                    public void onError(final SHARE_MEDIA share_media, final Throwable throwable) {
                        if (throwable != null) {
                            Log.d("throw", "throw:" + throwable.getMessage());
                        }
                        resultShareCallBack.resultShare("分享失败");

                    }

                    @Override
                    public void onCancel(final SHARE_MEDIA share_media) {
                        resultShareCallBack.resultShare("分享取消");
                    }
                })
                .share();
    }
}

2、初始化:
        ShareDialog shareDialog = new ShareDialog(this, utilsManage, new ShareDialog.resultShareCallBack() {
            @Override
            public void resultShare(String tv) {
                showToast(tv);
            }
        });

3、应用:

                if (goodsEntitiy.getData().getAlbum().size()>0){
                    imgUrl=goodsEntitiy.getData().getAlbum().get(0);
                }
                String userPhone = shareference.getUserLoginPhone();
                shareDialog.setUrl("https://phone.zhongshop.cn/#/pages/index/goods/detail?id=" + good_id+"&phone="+userPhone,
//                        goodsEntitiy.getData().getAlbum().get(0),
                        imgUrl,
                        goodsEntitiy.getData().getName(),
                        goodsEntitiy.getData().getStore_name() + "_" + goodsEntitiy.getData().getBrief()

                );
                shareDialog.showdialog();

 

发布了183 篇原创文章 · 获赞 135 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览