官方接入教程文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' }
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' }
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
//WechatShareTool类见下一步
new WechatShareTool(this).getApi().handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq arg0) {
// TODO 自动生成的方法存根
finish();
}
@Override
public void onResp(BaseResp resp) {
// TODO 自动生成的方法存根
switch (resp.errCode) { //根据需要的情况进行处理
case BaseResp.ErrCode.ERR_OK:
//正确返回
//ActivityTool.showToast为Toast的简单封装,开发中自行修改
ActivityTool.showToast(this, "分享成功");//后来微信改为无论分没分享都返回这一个
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
//用户取消
// ActivityTool.showToast(this, "取消分享");//取消也会返回ERR_OK
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
//认证被否决
ActivityTool.showToast(this, "分享失败,认证被否决");
break;
case BaseResp.ErrCode.ERR_SENT_FAILED:
//发送失败
ActivityTool.showToast(this, "发送失败");
break;
case BaseResp.ErrCode.ERR_UNSUPPORT:
//不支持错误
ActivityTool.showToast(this, "不支持错误");
break;
case BaseResp.ErrCode.ERR_COMM:
//一般错误
ActivityTool.showToast(this, "分享失败");
break;
default:
//其他不可名状的情况
break;
}
finish();
}
}
public class WechatShareTool {
private final static String WX_APP_ID = "your key";//WX_APP_ID自行修改为你的appkey
private IWXAPI api;
private int toFriend = SendMessageToWX.Req.WXSceneSession;//会话
private int toFriendCircle = SendMessageToWX.Req.WXSceneTimeline;//朋友圈
//SendMessageToWX.Req.WXSceneFavorite //分享到收藏
public WechatShareTool(Context context){
api = WXAPIFactory.createWXAPI(context, WX_APP_ID,false);
// 将应用的appId注册到微信
api.registerApp(WX_APP_ID);
}
public IWXAPI getApi(){
return api;
}
/*
* 是否安装了微信
*/
public boolean isWXAppInstalled(){
return api.isWXAppInstalled();
}
/**
* 微信文字分享
* @param text
* @param isNotToFriend 是否分享到朋友圈
*/
public void shareText(String text,boolean isNotToFriend){
//初始化一个 WXTextObject 对象,填写分享的文本内容
WXTextObject textObj = new WXTextObject();
textObj.text = text;
//用 WXTextObject 对象初始化一个 WXMediaMessage 对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = text;
shareToWX("text", msg, isNotToFriend);
}
/*
* 保证字符串唯一
*/
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
/**
* 分享图片
* @param file
*/
public void shareImage(String picturePath,boolean isNotToFriend){
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
//初始化 WXImageObject 和 WXMediaMessage 对象
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
//设置缩略图
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
bmp.recycle();
msg.thumbData = OtherTool.bmpToByteArray(thumbBmp, true);
shareToWX("img", msg, isNotToFriend);
}
/**
* 分享音乐
* @param title
* @param description
* @param musicUrl
* @param file
*/
public void shareMusic(String title,String description,String musicUrl,String picturePath,boolean isNotToFriend){
//初始化一个WXMusicObject,填写url
WXMusicObject music = new WXMusicObject();
music.musicUrl=musicUrl;
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
WXMediaMessage msg = getWXMediaMessage(title, description, music, bmp);
shareToWX("music",msg,isNotToFriend);
}
/**
* 分享视频
* @param title
* @param description
* @param viedoUrl
* @param file
*/
public void shareVideo(String title,String description,String viedoUrl ,String picturePath,boolean isNotToFriend){
//初始化一个WXVideoObject,填写url
WXVideoObject video = new WXVideoObject();
video.videoUrl = viedoUrl;
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
WXMediaMessage msg = getWXMediaMessage(title, description, video, bmp);
shareToWX("video",msg,isNotToFriend);
}
/*
* 设置mediaMessage和图片缩略图
*/
private WXMediaMessage getWXMediaMessage(String title,String description,IMediaObject media,Bitmap bmp){
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = media;
msg.title = title;
msg.description = description;
//设置缩略图
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 100, 100, true);
bmp.recycle();
msg.thumbData = bmpToByteArray(thumbBmp, true);//方法在最后面
return msg;
}
/*
* 分享
*/
private void shareToWX(String type,WXMediaMessage msg,boolean isNotToFriend){
//构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction(type);
req.message =msg;
if(isNotToFriend)
req.scene = toFriendCircle;
else
req.scene = toFriend;
req.userOpenId = WX_APP_ID;
//调用api接口,发送数据到微信
api.sendReq(req);
}
/*
* 图片转换
*/
private byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}