1.添加依赖
2.申请appid
3.在包下新建wxapi文件夹,并且新建WXEntryActivity.kt和WXPayEntryActivity.kt两个文件夹,后者是支付回调。
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.zf.dc.api.UriConstant
import com.zf.dc.showToast
import com.zf.dc.ui.activity.LoginActivity
import com.zf.dc.utils.LogUtils
class WXEntryActivity : Activity(), IWXAPIEventHandler {
/**
* 拿code请求以下链接获取access_token:
* "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"
* 获取用户个人信息:
* "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID"
*/
private var api: IWXAPI? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
api = WXAPIFactory.createWXAPI(this, UriConstant.WX_APP_ID, false)
api?.handleIntent(intent, this)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
api?.handleIntent(intent, this)
}
//应用请求微信的相应回调
override fun onResp(resp: BaseResp?) {
LogUtils.e(">>>>>>微信普通回调?")
LogUtils.e(">>>>>>WX登录普通回调结果:" + resp?.errCode + " 类型:" + resp?.type)
when (resp?.errCode) {
BaseResp.ErrCode.ERR_OK -> {
when (resp.type) {
ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX -> {
showToast("分享成功")
finish()
}
}
/** 除了登录才能成功往下执行,否则到这里就抛错跳出了。 */
val code = (resp as SendAuth.Resp).code
if (code.isNotEmpty()) {
//登录成功,清除全部活动,跳转到首页
val intent = Intent(this, LoginActivity::class.java)
intent.putExtra("code", code)
startActivity(intent)
}
finish()
}
BaseResp.ErrCode.ERR_USER_CANCEL -> {
showToast("已取消")
finish()
}
BaseResp.ErrCode.ERR_AUTH_DENIED -> {
showToast("已拒绝")
finish()
}
else -> {
showToast("失败")
finish()
}
}
}
//微信发送请求到应用的回调
override fun onReq(p0: BaseReq?) {
finish()
}
}
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.zf.dc.api.UriConstant
import com.zf.dc.showToast
import com.zf.dc.utils.bus.RxBus
class WXPayEntryActivity : Activity(), IWXAPIEventHandler {
private var api: IWXAPI? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
api = WXAPIFactory.createWXAPI(this, UriConstant.WX_APP_ID, false)
api?.handleIntent(intent, this)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
api?.handleIntent(intent, this)
}
/**
* 微信支付回调 //0 成功 展示成功页面
* //-1 错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
* //-2 用户取消 无需处理。发生场景:用户不支付了,点击取消,返回APP。
*/
//应用请求微信的相应回调
override fun onResp(resp: BaseResp?) {
when (resp?.errCode) {
BaseResp.ErrCode.ERR_OK -> {
when (resp.type) {
ConstantsAPI.COMMAND_PAY_BY_WX -> {
showToast("支付成功")
finish()
RxBus.getDefault().post(UriConstant.WX_PAY_SUCCESS, UriConstant.WX_PAY_SUCCESS)
}
}
finish()
}
BaseResp.ErrCode.ERR_USER_CANCEL -> {
showToast("已取消")
finish()
}
BaseResp.ErrCode.ERR_AUTH_DENIED -> {
showToast("已拒绝")
finish()
}
else -> {
showToast("支付失败")
finish()
}
}
}
//微信发送请求到应用的回调
override fun onReq(p0: BaseReq?) {
finish()
}
}
4.登录的代码
//微信登录
wxLogin.setOnClickListener {
val api = WXAPIFactory.createWXAPI(this, UriConstant.WX_APP_ID, true)
// 将应用的appId注册到微信
api.registerApp(UriConstant.WX_APP_ID)
if (!api.isWXAppInstalled) {
showToast("未安装微信")
} else {
val req = SendAuth.Req()
req.scope = "snsapi_userinfo"
req.state = "wechat_sdk_zf"
api.sendReq(req)
}
}
5.分享网页链接的代码
//分享
shareLayout.setOnClickListener {
val api = WXAPIFactory.createWXAPI(this, UriConstant.WX_APP_ID, true)
// 将应用的appId注册到微信
api.registerApp(UriConstant.WX_APP_ID)
if (!api.isWXAppInstalled) {
showToast("未安装微信")
} else {
/** 发送文字类型 */
val webObj = WXWebpageObject()
webObj.webpageUrl =
UriConstant.BASE_URL + "shop/detail?team_id=" + mGroupBean?.info?.team_id + "&goods_id=" + mGroupBean?.info?.goods_id
val msg = WXMediaMessage()
msg.mediaObject = webObj //消息对象
msg.title = "商城" //标题
msg.description = mGroupBean?.info?.goods_name
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.icon_dc_logo2)
msg.thumbData = WXBmpUtil.getBitmapBytes(bitmap, false)
val req = SendMessageToWX.Req()
req.transaction = buildTransaction("text")
req.message = msg
req.scene = mTargetScene
api.sendReq(req)
}
}
6.WXBmpUtil.java
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import java.io.ByteArrayOutputStream;
public class WXBmpUtil {
public static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean) {
Bitmap localBitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.RGB_565);
Canvas localCanvas = new Canvas(localBitmap);
int i;
int j;
if (bitmap.getHeight() > bitmap.getWidth()) {
i = bitmap.getWidth();
j = bitmap.getWidth();
} else {
i = bitmap.getHeight();
j = bitmap.getHeight();
}
while (true) {
localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0,
80, 80), null);
if (paramBoolean)
bitmap.recycle();
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
localBitmap.recycle();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
try {
localByteArrayOutputStream.close();
return arrayOfByte;
} catch (Exception e) {
// F.out(e);
}
i = bitmap.getHeight();
j = bitmap.getHeight();
}
}
}
7.支付代码
val api = WXAPIFactory.createWXAPI(this, UriConstant.WX_APP_ID, true)
api.registerApp(UriConstant.WX_APP_ID)
if (!api.isWXAppInstalled) {
showToast("未安装微信")
} else {
val req = PayReq()
req.appId = bean.appid
req.partnerId = bean.partnerid
req.prepayId = bean.prepayid
req.packageValue = bean.packageValue
req.nonceStr = bean.noncestr
req.timeStamp = bean.timestamp
req.sign = bean.sign
api.sendReq(req)
}