在java中向活动添加标志

ozxc1zmp  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(410)

我不是java开发人员,但我需要在我的应用程序(第三方库)中实现一些android本机模块。
我调用@reactmethod来处理付款。

@ReactMethod
void processPayment(Boolean showTransactionResult, Callback successCallback, Callback errorCallback) {
    Log.d("Message string", String.valueOf("Payment processing started "));
    ReactApplicationContext context = getReactApplicationContext();
    TransactionCallback callback = null; //?
    Activity currentActivity = getCurrentActivity();
    Log.d("Current activity", String.valueOf(currentActivity));
    try {
        makeSale(context, showTransactionResult, callback);
        successCallback.invoke("Callback : payment successful");
    } catch (IllegalViewOperationException e){
        errorCallback.invoke(e.getMessage());
    }
}

这个java函数应该在应用程序前面打开支付窗口。

void makeSale(ReactApplicationContext context, boolean showTransactionResult, TransactionCallback callback){
    Map<String, String> extras = new HashMap<>();
    extras.put("KEY_1", "Value 1");
    extras.put("KEY_2", "Value 2");
    PaymentSystem.getInstance().makeSale(context, true, extras, new TransactionCallback() {
        @Override
        public void onSuccess(String s, @Nullable @org.jetbrains.annotations.Nullable Map<String, String> map) {
            //Sale successful
            //data is transaction key
            //extras is the Map passed in makeSale()
        }

        @Override
        public void onFailure(PaymentSystemException e, @Nullable @org.jetbrains.annotations.Nullable Map<String, String> map) {
            //Sale failed
        }
    });
}

但我收到了以下错误消息:

E/unknown:ReactNative: Exception in native call
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:1018)
    at android.app.ContextImpl.startActivity(ContextImpl.java:994)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)
    at digital.paynetics.paymentSystem.paymentSystemSdkMock.launchCallbackMockActivity(:5)
    at digital.paynetics.paymentSystem.paymentSystemSdkMock.makeSale(:2)
    at com.myapp.paymentSystemModule.makeSale(paymentSystemModule.java:118)
    at com.myapp.paymentSystemModule.processPayment(paymentSystemModule.java:82)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
    at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
    at android.os.Looper.loop(Looper.java:223)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
    at java.lang.Thread.run(Thread.java:923)

我在互联网上找到了一个解决方案,它可以很容易地将标志添加到活动的意图中。但是我不知道怎么做。我认为intent是第三方库的一部分。
请帮帮我。如果只是一些java问题,很抱歉出现转储问题。

wqnecbli

wqnecbli1#

尝试使用getcurrentactivity打开新活动,而不是getapplicationcontext。
例如。

makeSale(currentActivity, showTransactionResult, callback);

相关问题