/**
* 向支付宝发送支付请求
*
* @param strOrderInfo
* 订单信息
* @param callback
* 回调handler
* @param myWhat
* 回调信息
* @param activity
* 目标activity
* @return
*/
public boolean pay(final String strOrderInfo, final Handler callback,
final int myWhat, final Activity activity) {
if (mbPaying)
return false;
mbPaying = true;
//
mActivity = activity;
// bind the service.
// 绑定服务
if (mAlixPay == null) {
// 绑定安全支付服务需要获取上下文环境,
// 如果绑定不成功使用mActivity.getApplicationContext().bindService
// 解绑时同理
mActivity.getApplicationContext().bindService(
new Intent(IAlixPay.class.getName()), mAlixPayConnection,
Context.BIND_AUTO_CREATE);
}
// else ok.
// 实例一个线程来进行支付
new Thread(new Runnable() {
public void run() {
try {
// wait for the service bind operation to completely
// finished.
// Note: this is important,otherwise the next mAlixPay.Pay()
// will fail.
// 等待安全支付服务绑定操作结束
// 注意:这里很重要,否则mAlixPay.Pay()方法会失败
synchronized (lock) {
if (mAlixPay == null)
lock.wait();
}
// register a Callback for the service.
// 为安全支付服务注册一个回调
mAlixPay.registerCallback(mCallback);
// call the MobileSecurePay service.
// 调用安全支付服务的pay方法
String strRet = mAlixPay.Pay(strOrderInfo);
BaseHelper.log(TAG, "After Pay: " + strRet);
// set the flag to indicate that we have finished.
// unregister the Callback, and unbind the service.
// 将mbPaying置为false,表示支付结束
// 移除回调的注册,解绑安全支付服务
mbPaying = false;
mAlixPay.unregisterCallback(mCallback);
mActivity.getApplicationContext().unbindService(
mAlixPayConnection);
// send the result back to caller.
// 发送交易结果
Message msg = new Message();
msg.what = myWhat;
msg.obj = strRet;
callback.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
// send the result back to caller.
// 发送交易结果
Message msg = new Message();
msg.what = myWhat;
msg.obj = e.toString();
callback.sendMessage(msg);
}
}
}).start();
return true;
}