Appcelerator 自定义模块接收 onActivityResult 的回调
我正在尝试在 Appcelerator 中为新的 Android Square API 创建自定义模块。我已经按照我想要的方式拥有了一切,但主要问题是我希望能够在付款失败时通知呼叫者付款成功。 Square API 是这样说的:
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());
}
然后在实际运行付款的方法中,我基本上尝试使用 TiActivitySupportHelper 中的 registerResultHandler 将传入的回调设置为当前活动的 onResult 处理程序班级。
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!';
});
I'm trying to create a custom module in Appcelerator for the new Square API for Android. I have everything the way I want it, but the main problem is that I want to be able to notify the caller that the payment was successful for if it failed. The Square API says this:
After Square finishes, Android invokes Activity.onActivityResult() on the activity passed to the constructor. The request code passed to this method will be passed to onActivityResult(). The result code is Activity.RESULT_CANCELED if the payment was canceled or Activity.RESULT_OK if the payment succeeded.
I've been passing the TiContext.currentActivity to the constructor:
public SquareModule(TiContext tiContext) {
super(tiContext);
ourSquare = new Square(tiContext.getActivity());
}
And then in the method that actually runs the payment, I have this that basically tries to set the passed in callback to the onResult handlers of the current activity using the registerResultHandler in the TiActivitySupportHelper class.
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);
}
The main module class implements TiActivityResultHandler and implements onResult and onError. These methods are not being called at all. And of course the passed in method isn't being called either.
For completeness, see the implementation of the onResult and onError handlers:
@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);
}
And also see the Appcelerator JS calling the method in the module:
square.runPayment(2, 'Testing123', function(e) {
label1.text = 'Payment Successful!';
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些遇到这个问题的人。答案可以在此处的模块中找到:
https://github.com/hidef/Appcelerator-Square-Module(请参阅 LaunchSquare .java 类)
基本上,我使用了我创建的 Activity 对象来接收 Square API 的 onResult 更新。然后,我能够将其干净地传递回模块类,并通过回调将其传回调用应用程序。
For those that come upon this question. The answer can be found in the module here:
https://github.com/hidef/Appcelerator-Square-Module (see the LaunchSquare.java class)
Basically, I used an Activity object that I created to receive the Square API's onResult update. I then was able to pass that back cleanly to the module class and hand it back via callback to the calling application.