!!!第一处:
官方demo中AppRegister是下面这样的:
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI api = WXAPIFactory.createWXAPI(context, null);
api.registerApp(Constants.APP_ID);
}
}
为了不出错,请修改成下面这样:
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI api = WXAPIFactory.createWXAPI(context, Constant.APP_ID,false);
api.registerApp(Constants.APP_ID);
}
}
app_ID传空时,此方法实现了去AndroidManifest.xml中找到已配置好的APP_ID,所以官方Demo直接传了空,
但是,从AndroidManifest.xml获取APP_ID并不是所有情况下都能成功,至少我这里遇到的情况是获取不到的,所以导致请求时APP_ID一直为空
因此,为了确保不被坑,请使用上面第二段代码,主动传入APP_ID;
!!!第二处:
在AndroidManifest.xml中配置所有跟微信支付回调相关的路径时:
我们平时自己创建的Activity之类的路径一般都是缺省了包名的,如下:
<receiver android:name=".wxapi.AppRegister">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP"/>
</intent-filter>
</receiver>
还是这句话,为了不被坑,针对跟微信支付回调相关的请务必把包名补全,如下这样,不然微信回调时可能会找不到页面:
<receiver android:name="com.xykmrxx.xmr.wxapi.AppRegister">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP"/>
</intent-filter>
</receiver>
另附上集成微信支付的全部代码:
第一步当然还是引入微信JAR包 ,该包可以从官方微信支付demo中获得。
然后新建包名wxapi,在wxapi下新建回调页面WXPayEntryActivity,命名和存放路径要求不能错:
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private static final String TAG = "wx";
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, Constant.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 req) {}
@Override
public void onResp(BaseResp resp) {
//resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX时表示是微信支付返回
switch (resp.errCode){
case 0:
Intent in=new Intent();
in.setClass(WXPayEntryActivity.this, PayFinishedActivity.class);
startActivity(in);
finish();
break;
case -1:
Log.d(TAG, "onResp: -1");
break;
case -2:
Log.d(TAG, "onResp: -2");
break;
}
}
然后再新增AppRegister,这个存放路径没要求:
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI msgApi = WXAPIFactory.createWXAPI(context, Constant.APP_ID,false);
msgApi.registerApp(Constant.APP_ID);
}
}
然后在AndroidManifest.xml中配置:
<!-- 微信支付 -->
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="替换成你的APP_ID"/>
<!-- appId -->
</intent-filter>
</activity>
<receiver android:name="com.xykmrxx.xmr.wxapi.AppRegister">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP"/>
</intent-filter>
</receiver>
最后在需要发起微信支付的页面调用:
IWXAPI msgApi;
msgApi = WXAPIFactory.createWXAPI(this, Constant.APP_ID,false);
//paymentApp是我从服务器请求返回的json字符串,其中parameter字段中存放的是微信支付需要的相关参数
try {
JSONObject jso = new JSONObject(paymentApp.getParameter());
PayReq request = new PayReq();
request.appId = jso.getString("appid");
request.nonceStr = jso.getString("noncestr");
request.packageValue = jso.getString("package");
request.partnerId = jso.getString("partnerid");
request.prepayId = jso.getString("prepayid");
request.timeStamp = jso.getString("timestamp");
request.sign = jso.getString("sign");
msgApi.registerApp(Constant.APP_ID);
msgApi.sendReq(request);
集成代码就这些!
如果还不懂,可以参考:http://blog.csdn.NET/leoleohan/article/details/48158411