Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Appcelerator自定义模块接收onActivityResult的回调_Java_Android_Appcelerator_Appcelerator Mobile - Fatal编程技术网

Java Appcelerator自定义模块接收onActivityResult的回调

Java Appcelerator自定义模块接收onActivityResult的回调,java,android,appcelerator,appcelerator-mobile,Java,Android,Appcelerator,Appcelerator Mobile,我正在尝试在Appcelerator中为Android的新SquareAPI创建一个自定义模块。我有一切我想要的方式,但主要的问题是,我想能够通知来电者,付款是成功的,如果它失败了。Square API说: Square完成后,Android对传递给构造函数的活动调用Activity.onActivityResult()。传递给此方法的请求代码将传递给onActivityResult()。如果付款已取消,则结果代码为Activity.result\u Cancelled;如果付款成功,则结果代码

我正在尝试在Appcelerator中为Android的新SquareAPI创建一个自定义模块。我有一切我想要的方式,但主要的问题是,我想能够通知来电者,付款是成功的,如果它失败了。Square API说:

Square完成后,Android对传递给构造函数的活动调用Activity.onActivityResult()。传递给此方法的请求代码将传递给onActivityResult()。如果付款已取消,则结果代码为Activity.result\u Cancelled;如果付款成功,则结果代码为Activity.result\u OK

我一直在将TiContext.currentActivity传递给构造函数:

public SquareModule(TiContext tiContext) {
    super(tiContext);

    ourSquare = new Square(tiContext.getActivity());
}   
然后在实际运行支付的方法中,我有一个基本上尝试使用TiActivitySupperHelper类中的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!';
});

对于那些提出这个问题的人。答案可在以下模块中找到: (请参阅LaunchSquare.java类)


基本上,我使用创建的活动对象来接收Square API的onResult更新。然后,我可以将其干净地传递回模块类,并通过回调将其传递回调用应用程序。

您能解释更多吗?我有一个类似的问题,我创建了一个.aar安卓模块,所有模块都有onActivityResult函数,该函数没有被调用,我假设是它,因为该模块不是主要活动,所以我需要从我的Alloy应用截取该函数