今天做了微信支付,总结一下流程:
1.创建一个广播
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);
// 将该app注册到微信
msgApi.registerApp(WXPayConfig.APP_ID);
}
}
2.在包名下创建wxapi包,创建WXPayEntryActivity类,用于接收微信支付结果的回调
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, View.OnClickListener {
// 标题栏返回箭头
private ImageView iv_wx_pay_callback_back;
private IWXAPI api;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq req) {
}
@Override
public void onResp(BaseResp resp) {
Log.d("TAG", "onPayFinish, errCode = " + resp.errCode);
/**
* 0表示支付成功,-1表示支付终端,-2表示支付失败
*/
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("微信支付结果:" + String.valueOf(resp.errCode));
builder.show();
}
}
3.在清单文件中配置
<!--支付宝支付-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-per