app分享功能

背景
现在市场上的app基本都具有分享功能,为了避免之后又重复造轮子,决定记录一下。点击分享,弹出一框框(PopupWindow),再选择分享平台就这样。

配置
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&token=&lang=zh_CN

WeChatShare代码:

public class WeChatShare extends ShareManager {
    private IWXAPI api;
    private WXWebpageObject webpage;
    private SendMessageToWX.Req req;
    private Context context;
    public static WeChatShare share;

    public WeChatShare(Context context) {
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, BuildConfig.WEIXIN_APPID);
    }

    private void creatData(String descrip, String webId, ArrayList<String> images) {
        webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://www.baidu.com" + webId;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = context.getResources().getString(R.string.app_name);
        msg.description = descrip;
        Bitmap thumb = null;
        try {
            thumb = Glide.with(context).load(images.get(0)).asBitmap().centerCrop()
                    .into(500, 500)
                    .get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        msg.thumbData = BitmapUtils.bmpToByteArray(thumb, true);

        req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;

    }

    public static WeChatShare getInstance(Context context) {
        if (share == null) {
            share = new WeChatShare(context);
        }
        return share;
    }

    public void ShareToWeChatSeesion(String descrip, String webId, ArrayList<String> images) {
        if (api.isWXAppInstalled()) {
            creatData(descrip, webId, images);
            req.scene = SendMessageToWX.Req.WXSceneSession;
            api.sendReq(req);
        } else {
            ((BaseActivity) context).showToast("未安装微信客户端");
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值