Android Service 支付宝例子中不理解的地方
public class PayService extends Service {
private static final String TAG = "PayService";
private ThirdPartPayImpl mThirdPartPay;
@Override
public IBinder onBind(Intent intent) {
String action = intent.getAction();
Log.d(TAG, "onBind");
if (action != null && "com.alibaba.alipay.THIRD_PART_PAY".equals(action)) {
mThirdPartPay = new ThirdPartPayImpl();
//----------------------------------------------
}
return mThirdPartPay;
//------------------------------------------
}
public class PayAction extends Binder {
public void pay(float payMoney) {
Log.d(TAG, "PayAction Money --> " + payMoney);
if (mThirdPartPay != null) {
mThirdPartPay.paySuccess();
}
}
public void onUserCancel() {
mThirdPartPay.payFailed(1, "userCancel");
}
}
private class ThirdPartPayImpl extends ThirdPartPayAction.Stub {
private ThirdPartPayResult mCallback;
@Override
public void requestPay(String orderInfo, float payMoney, ThirdPartPayResult callback) {
this.mCallback = callback;
Intent intent = new Intent();
intent.setClass(PayService.this, PayActivity.class);
intent.putExtra(Constants.KEY_BILL_INFO, orderInfo);
intent.putExtra(Constants.KEY_PAY_MONEY, payMoney);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
public void paySuccess() {
try {
if (mCallback != null) {
mCallback.onPaySuccess();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void payFailed(int errorCode, String errorMsg) {
try {
if (mCallback != null) {
mCallback.onPayFailes(errorCode, errorMsg);
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
上面的和下面的,如果action正常的话,都是返回mThirdPartPay,但上面的写法,在其它app调用的时候,调用不了mThirdPartPay中的requetPay,,,,也没报错,,
不理解。
都是回返mThirdPartPay,一个在if里面,一个在if外面,结果却不一样,
下面的是成功的,在另一个app,点击按钮, 调用mThirdPartPay 的requestPay方法 ,成功跳转到指定界面
上面的,在另一个app中调用没反应,,,,
还有下面中返回的 new PayAction(),它在action正确的时候不会调用,那它是干嘛用的
public class PayService extends Service {
private static final String TAG = "PayService";
private ThirdPartPayImpl mThirdPartPay;
@Override
public IBinder onBind(Intent intent) {
String action = intent.getAction();
Log.d(TAG, "onBind");
if (action != null && "com.alibaba.alipay.THIRD_PART_PAY".equals(action)) {
mThirdPartPay = new ThirdPartPayImpl();
//----------------------------------------------
return mThirdPartPay;
}
return new PayAction();
//------------------------------------------
}
public class PayAction extends Binder {
public void pay(float payMoney) {
Log.d(TAG, "PayAction Money --> " + payMoney);
if (mThirdPartPay != null) {
mThirdPartPay.paySuccess();
}
}
public void onUserCancel() {
mThirdPartPay.payFailed(1, "userCancel");
}
}
private class ThirdPartPayImpl extends ThirdPartPayAction.Stub {
private ThirdPartPayResult mCallback;
@Override
public void requestPay(String orderInfo, float payMoney, ThirdPartPayResult callback) {
this.mCallback = callback;
Intent intent = new Intent();
intent.setClass(PayService.this, PayActivity.class);
intent.putExtra(Constants.KEY_BILL_INFO, orderInfo);
intent.putExtra(Constants.KEY_PAY_MONEY, payMoney);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
public void paySuccess() {
try {
if (mCallback != null) {
mCallback.onPaySuccess();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void payFailed(int errorCode, String errorMsg) {
try {
if (mCallback != null) {
mCallback.onPayFailes(errorCode, errorMsg);
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
都是回返mThirdPartPay,一个在if里面,一个在if外面,结果却不一样,
这个问题,没看懂。什么不一样,什么结果不一样呢?
还有下面图片中返回的 newPayAction,它在action正确的时候不会调用,那它是干嘛用的,这个意思是,你可以根据不同的action返回不同的binder,你的payAction是BInder,而你的ThiredPartPlayImpl也是Binder,你还可以多写几个呢。
我已经回你第二个问题了。
至于第一个问题,你得看你另外一边返回的对象了。
打断点就可以看到,我猜测你的有可能是空。你的log也写得不好。怎么不把action打出来呢?
你说你的是对的,那可不一定。
加Log,打断点,看对像就知道为什么调用不成功了。