微信登录
//注意放置的目录位置 微信sdk要求
//compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
IWXAPI wxApi;
public static final String APP_ID = "微信后台申请的";
String code = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wxApi = WXAPIFactory.createWXAPI(this, APP_ID, true);
wxApi.registerApp(APP_ID);
wxApi.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
if (baseResp instanceof SendAuth.Resp) {
SendAuth.Resp resp = (SendAuth.Resp) baseResp;
code = resp.code;
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
自己业务需求
//wechatLogin(code);
}
});
return;
}
}
finish();
}
private void wechatLogin(String wxAuthCode) {
// ClipboardManager cm = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
// cm.setText(code);
// Toast.makeText(this, "微信code已复制到剪切板", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setClass(this, LoginActivity.class);
intent.putExtra("snsType", LoginActivity.SNS_TYPE_WEXIN);
intent.putExtra("wxAuthCode", wxAuthCode);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}
在activity里面调用
wxApi = WXAPIFactory.createWXAPI(this, WXEntryActivity.APP_ID, true);
wxApi.registerApp(WXEntryActivity.APP_ID);
//使用
private void callWeixin() {
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.transaction = UUID.randomUUID().toString().replace("-", "");
req.state = UUID.randomUUID().toString().replace("-", "");
wxApi.sendReq(req);
}
微信分享
1.创建微信分享实体类
public class WeChatShareModel {
private String url;
private String title;
private String content;
private int img;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
}
开始分享
WeChatShareModel weChatShareModel = new WeChatShareModel();
weChatShareModel.setTitle("");
weChatShareModel.setContent("");
weChatShareModel.setUrl("");
weChatShareModel.setImg(R.mipmap.ic_launcher);
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = weChatShareModel.getUrl();
WXMediaMessage wxMediaMessage = new WXMediaMessage(webpageObject);
wxMediaMessage.title = weChatShareModel.getTitle();
wxMediaMessage.description = weChatShareModel.getContent();
Bitmap thump =
BitmapFactory.decodeResource(context.getResources(),weChatShareModel.getImg());
wxMediaMessage.thumbData = bmpToByteArray(createBitmapThumbnail(thump),true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = wxMediaMessage;
req.scene = SendMessageToWX.Req.WXSceneSession; // 目前支持会话
// req.scene = SendMessageToWX.Req.WXSceneTimeline;
wxApi.sendReq(req);
public static Bitmap createBitmapThumbnail(Bitmap bitMap) {
int width = bitMap.getWidth();
int height = bitMap.getHeight();
// 设置想要的大小
int newWidth = 99;
int newHeight = 99;
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newBitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
return newBitMap;
}
/**
* 图片转换成byte数组
* @param thump
* @param needRecycle
* @return
*/
private static byte[] bmpToByteArray(Bitmap thump,boolean needRecycle){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thump.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if (needRecycle) {
thump.recycle();
}
byte[] data = baos.toByteArray();
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
private static String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}