我想在Appcelerator中爲Android的新Square API創建一個自定義模塊。我擁有所有我想要的方式,但主要問題是我希望能夠通知呼叫方,如果支付失敗,支付是成功的。 Square API說:Appcelerator自定義模塊接收onActivityResult回調
在Square完成後,Android會在傳遞給構造函數的Activity上調用Activity.onActivityResult()。傳遞給此方法的請求代碼將傳遞給onActivityResult()。如果付款被取消,結果代碼爲Activity.RESULT_CANCELED;如果付款成功,結果代碼爲Activity.RESULT_OK。
我一直在傳遞TiContext.currentActivity的構造器:
public SquareModule(TiContext tiContext) {
super(tiContext);
ourSquare = new Square(tiContext.getActivity());
}
然後在實際運行支付方式,我有這個基本上嘗試設置在回調傳遞給當前活動的onResult處理程序使用TiActivitySupportHelper類中的registerResultHandler。
public void runPayment(KrollInvocation invocation, int price, String description, KrollCallback handler) {
Log.i(LCAT, "runPayment called");
// Register the passed in function as a handler on the onResult stack
this.resultCallback = handler;
Activity activity = invocation.getTiContext().getActivity();
TiActivitySupportHelper support = new TiActivitySupportHelper(activity);
int code = support.getUniqueResultCode();
support.registerResultHandler(code, this);
// Some of the payment work here
ourSquare.squareUp(Bill.containing(advice), code);
}
主模塊類實現TiActivityResultHandler並實現onResult和onError。這些方法根本沒有被調用。當然,傳入的方法也沒有被調用。
爲了完整起見,看到onResult和onError的處理程序執行:
@Override
public void onResult(Activity activity, int requestCode, int resultCode, Intent data)
{
Log.i(LCAT, "onResult Called");
if (resultCallback == null) return;
KrollDict event = new KrollDict();
event.put(TiC.EVENT_PROPERTY_REQUEST_CODE, requestCode);
event.put(TiC.EVENT_PROPERTY_RESULT_CODE, resultCode);
event.put(TiC.EVENT_PROPERTY_INTENT, new IntentProxy(getTiContext(), data));
event.put(TiC.EVENT_PROPERTY_SOURCE, this);
resultCallback.callAsync(event);
}
@Override
public void onError(Activity activity, int requestCode, Exception e)
{
Log.i(LCAT, "onError Called");
if (resultCallback == null) return;
KrollDict event = new KrollDict();
event.put(TiC.EVENT_PROPERTY_REQUEST_CODE, requestCode);
event.put(TiC.EVENT_PROPERTY_ERROR, e.getMessage());
event.put(TiC.EVENT_PROPERTY_SOURCE, this);
resultCallback.callAsync(event);
}
而且也看到了Appcelerator的JS調用模塊中的方法:
square.runPayment(2, 'Testing123', function(e) {
label1.text = 'Payment Successful!';
});