废话概要:
1.想要接入微信的接口,首先你的有一个微信开发者账号微信开发者平台
2.管理中心->创建移动应用->提交App应用详情->审核->审核通过->获取APP SECRET
注意:
在创建移动应用时需要提交签名,签名获取方法如下:
安装签名生成工具,输入在App包名,点击Get Singnature即可
微信提供的参考资源:
参考代码
语音SDK+Demo+开发文档
图像SDK+Demo+开发文档
卡券SDK+开发文档
开始撸码:
1、导包:
在build.gradle文件中,添加如下依赖即可:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'}
2、创建微信管理类:
public class WXshare {
public static final String APP_ID = "wx4656bd61a787****";
public static final String ACTION_SHARE_RESPONSE = "action_wx_share_response";
public static final String EXTRA_RESULT = "result";
private final Context context;
private final IWXAPI api;//个对象是专门用来向微信发送数据的一个重要接口,使用强引用持有,所有的信息发送都是基于这个对象的
private OnResponseListener listener;
private ResponseReceiver receiver;
public WXshare(Context context) {
api = WXAPIFactory.createWXAPI(context, APP_ID);
this.context = context;
}
public WXshare register() {
// 向微信注册你的App
api.registerApp(APP_ID);
receiver = new ResponseReceiver();
IntentFilter filter = new IntentFilter(ACTION_SHARE_RESPONSE);
context.registerReceiver(receiver, filter);
return this;
}
public void unregister() {
// 向微信注销你的App
try {
api.unregisterApp();
context.unregisterReceiver(receiver);
} catch (Exception e) {
e.printStackTrace();
}
}
public WXshare shareText(String text) {
//向微信分享文字
WXTextObject textObj = new WXTextObject();
textObj.text = text;
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = text;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
boolean result = api.sendReq(req);
return this;
}
public WXshare shareUrl(int flag,Context context,String url,String title,String descroption){
//flag:0代表分享到微信好友,1代表分享到朋友圈
//初始化一个WXWebpageObject填写url
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = url;
//用WXWebpageObject对象初始化一个WXMediaMessage,天下标题,描述
WXMediaMessage msg = new WXMediaMessage(webpageObject);
msg.title = title;
msg.description = descroption;
//这块需要注意,图片的像素千万不要太大,不然的话会调不起来微信分享,或者直接参考下文的sharePicture
Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_logo);
msg.setThumbImage(thumb);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = flag==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;
api.sendReq(req);
return this;
}
public void sharePicture(Bitmap bitmap, int shareType) {
//分享图片
//flag:0代表分享到微信好友,1代表分享到朋友圈
WXImageObject imgObj = new WXImageObject(bitmap);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, true);
bitmap.recycle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
msg.thumbData = byteArray; //设置缩略图
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("imgshareappdata");
req.message = msg;
req.scene = shareType;
api.sendReq(req);
}
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public IWXAPI getApi() {
return api;
}
public void setListener(OnResponseListener listener) {
this.listener = listener;
}
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
private class ResponseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Response response = intent.getParcelableExtra(EXTRA_RESULT);
String result;
if (listener != null) {
if (response.errCode == BaseResp.ErrCode.ERR_OK) {
listener.onSuccess();
} else if (response.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
listener.onCancel();
} else {
switch (response.errCode) {
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "发送被拒绝";
break;
case BaseResp.ErrCode.ERR_UNSUPPORT:
result = "不支持错误";
break;
default:
result = "发送返回";
break;
}
listener.onFail(result);
}
}
}
}
public static class Response extends BaseResp implements Parcelable {
public int errCode;
public String errStr;
public String transaction;
public String openId;
private int type;
private boolean checkResult;
public Response(BaseResp baseResp) {
errCode = baseResp.errCode;
errStr = baseResp.errStr;
transaction = baseResp.transaction;
openId = baseResp.openId;
type = baseResp.getType();
checkResult = baseResp.checkArgs();
}
@Override
public int getType() {
return type;
}
@Override
public boolean checkArgs() {
return checkResult;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.errCode);
dest.writeString(this.errStr);
dest.writeString(this.transaction);
dest.writeString(this.openId);
dest.writeInt(this.type);
dest.writeByte(this.checkResult ? (byte) 1 : (byte) 0);
}
protected Response(Parcel in) {
this.errCode = in.readInt();
this.errStr = in.readString();
this.transaction = in.readString();
this.openId = in.readString();
this.type = in.readInt();
this.checkResult = in.readByte() != 0;
}
public static final Creator<Response> CREATOR = new Creator<Response>() {
@Override
public Response createFromParcel(Parcel source) {
return new Response(source);
}
@Override
public Response[] newArray(int size) {
return new Response[size];
}
};
}}
3、监听返回接口:
public interface OnResponseListener {
void onSuccess();
void onCancel();
void onFail(String message); }
4、使用:
public class WXShareActivity extends AppCompatActivity implements IWXAPIEventHandler {
private WXshare wxShare;
private Button bluth_btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluth);
bluth_btnStart= (Button) findViewById(R.id.bluth_btnStart);
wxShare = new WXshare(this);
wxShare.register();
wxShare.setListener(new OnResponseListener() {
@Override
public void onSuccess() {
Log.e("TAG", "onSuccess");
}
@Override
public void onCancel() {
Log.e("TAG", "onCancel");
}
@Override
public void onFail(String message) {
Log.e("TAG", "" + message);
}
});
final WXshare share = new WXshare(this);
bluth_btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 得到当前view所在view结构中的根view
View vv = v.getRootView();
// 设置属性
vv.setDrawingCacheEnabled(true);
// 取得位图
share.sharePicture(vv.getDrawingCache(), 0);
}
});
}
@Override
protected void onDestroy() {
wxShare.unregister();
super.onDestroy();
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
Intent intent = new Intent(WXshare.ACTION_SHARE_RESPONSE);
intent.putExtra(WXshare.EXTRA_RESULT, new WXshare.Response(baseResp));
sendBroadcast(intent);
}}
注意:
当我们撸完代码,发现点击分享的时候,App动了一下(感觉有跳转,然后没动静了),恭喜大兄弟,开机重启试一下,具体原因不得而知,猜测:大胆猜测是因为release和debug版的原因