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