1.build.gradle文件中增加:

compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'

2. 代码目录下增加如下类(WXPayEntryActivity.java,注意要放在wxapi包下):

package com.test.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

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;


/**
* 微信支付回调类
*/
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID);
api.handleIntent(getIntent(), this);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}

@Override
public void onReq(BaseReq baseReq) {}

@Override
public void onResp(BaseResp baseResp) {
Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
this.finish();
}

// private boolean isSuccess(BaseResp baseResp) {
// if (baseResp != null && baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
// if (baseResp.errCode == 0) {
// return true;
// }
// }
// return false;
// }
}

3.在manifest文件中配置回调

<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>

4.调用函数

    关键点:这个函数的入参map数据是来自服务器端和微信通信之后获取到的信息(微信支付不是单单客户端就可以完成的,需要服务器配合一起完成。)

public void gotoWeixinPay(Context context, Map<String, Object> map) {
String weixinAppId = WEIXIN_APP_ID;
final IWXAPI wxApi = WXAPIFactory.createWXAPI(context, weixinAppId);
wxApi.registerApp(weixinAppId);
final String appid = map.get("appid").toString();
final String partnerId = map.get("parterid").toString();
final String prepayId = map.get("prepayid").toString();
final String packageValue = map.get("packageValue").toString();
final String nonceStr = map.get("noncestr").toString();
final String timeStamp = map.get("timestamp").toString();
final String sign = map.get("sign").toString();

Runnable runnable = new Runnable() {
@Override
public void run() {
PayReq req = new PayReq();
req.appId = appid;
req.partnerId = partnerId;
req.prepayId = prepayId;
req.packageValue = packageValue;
req.nonceStr = nonceStr;
req.timeStamp = timeStamp;
req.sign = sign;
wxApi.sendReq(req);
}
};
Thread payThread = new Thread(runnable);
payThread.start();
}

附录:

微信官方文档:

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5