gpt4 book ai didi

java - 使用 pyjnius 在 python 中创建 Android Activity 上下文

转载 作者:太空狗 更新时间:2023-10-29 13:04:54 27 4
gpt4 key购买 nike

我正在使用以下代码将 braintrees 嵌入式 UI 支付方法导入我的 python kivy 应用程序。

from jnius import autoclass
from jnius import cast
from android import activity

Intent = autoclass('android.content.Intent')
PythonActivity = autoclass('org.renpy.android.PythonActivity')

DropInRequest = autoclass('com.braintreepayments.api.dropin.DropInRequest')

#Global instance
instance = None
REQUEST = 1
RESULT_OK = 1

def onBraintreeSubmit(token):
global instance

def on_activity_result(request, response, data):
global instance
if request == REQUEST:
print response
if response == RESULT_OK:
result = instance.getParcelableExtra(instance.EXTRA_DROP_IN_RESULT)
nonce = result.getPaymentMethodNonce()
print nonce
return nonce

activity.bind(on_activity_result=on_activity_result)
instance = DropInRequest()
instance.clientToken(token)
intent = instance.getIntent(activity.this)
PythonActivity.mActivity.startActivityForResult(intent,REQUEST)

它试图模仿这个 Java 代码

DropInRequest dropInRequest = new DropInRequest()
.clientToken(mClientToken);
startActivityForResult(dropInRequest.getIntent(context), DROP_IN_REQUEST);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == DROP_IN_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
String paymentMethodNonce = result.getPaymentMethodNonce().getNonce();
// send paymentMethodNonce to your server
} else if (resultCode == Activity.RESULT_CANCELED) {
// canceled
} else {
// an error occurred, checked the returned exception
Exception exception = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}

我的问题是我需要将上下文发送到 DropInRequest 类的 getIntent 函数,但我不知道如何使用 python-for-andriod 和 pyjnius 创建 Activity 上下文。下面的 getIntent 的 Java 方法。

public Intent getIntent(Context context) {
return new Intent(context, DropInActivity.class)
.putExtra(EXTRA_CHECKOUT_REQUEST, this);
}

我想我需要将它包含到我的 list 中并使用 com.braintreepayments.api.BraintreeBrowserSwitchActivity 作为我的 Activity ,但我不确定如何从中获取上下文。

<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="${applicationId}.braintree" />
</intent-filter>
</activity>

我认为应该调用这个类,它是一个 BrowserSwitchActivity,我需要从 BrowserSwitchActivity 获取上下文

package com.braintreepayments.api;

import com.braintreepayments.browserswitch.BrowserSwitchActivity;

/**
* Helper Activity that captures the response when browser switch completes.
*/
public class BraintreeBrowserSwitchActivity extends BrowserSwitchActivity {
}

在这里结束,将类扩展到 Activity 类

package com.braintreepayments.browserswitch;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;

/**
* <a href="https://developer.android.com/guide/topics/manifest/activity-element.html#lmode">singleTask</a>
* Activity used to receive the response from a browser switch. This Activity contains no UI and
* finishes during {@link Activity#onCreate(Bundle)}.
*/
public class BrowserSwitchActivity extends Activity {

private static Uri sReturnUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

sReturnUri = null;
if (getIntent() != null && getIntent().getData() != null) {
sReturnUri = getIntent().getData();
}

finish();
}

/**
* @return the uri returned from the browser switch, or {@code null}.
*/
@Nullable
public static Uri getReturnUri() {
return sReturnUri;
}

/**
* Clears the return uri.
*/
public static void clearReturnUri() {
sReturnUri = null;
}
}

所以我一直在思考如何从此 Activity 中获取上下文?

更新我已经更改了我的 python 代码,使用此代码在我的 python 代码内的 java 之外执行 DropInRequest.getIntent() 方法。

from jnius import autoclass
from jnius import cast
from android import activity
from kivy.context import get_current_context

context = autoclass('android.content.Context')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
PythonActivity = autoclass('org.kivy.android.PythonActivity')

DropInRequest = autoclass('com.braintreepayments.api.dropin.DropInRequest')
BrowserSwitchActivity = autoclass('com.braintreepayments.api.BraintreeBrowserSwitchActivity')

#Global instance
instance = None
REQUEST = 1
RESULT_OK = 1

def onBraintreeSubmit(token):
global instance

def on_activity_result(request, response, data):
global instance
if request == REQUEST:
print response
if response == RESULT_OK:
result = instance.getParcelableExtra(instance.EXTRA_DROP_IN_RESULT)
nonce = result.getPaymentMethodNonce()
print nonce
return nonce

currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
context = cast('android.content.Context', currentActivity.getApplicationContext())
activity.bind(on_activity_result=on_activity_result)
instance = DropInRequest()
instance.clientToken(token)
mapintent = Intent()
mapintent.setClassName(context,'com.braintreepayments.api.dropin.DropInRequest')
mapintent.putExtra("com.braintreepayments.api.EXTRA_CHECKOUT_REQUEST","EXTRA_CHECKOUT_REQUEST")

currentActivity.startActivityForResult(mapintent,REQUEST)

并将其添加到我的 list 中

<activity android:name="com.braintreepayments.api.dropin.DropInRequest" >
</activity>

我似乎正在取得进展,但当我运行应用程序时出现此错误。

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.test.myapp/com.braintreepayments.api.dropin.DropInRequest}: java.lang.ClassCastException: com.braintreepayments.api.dropin.DropInRequest cannot be cast to android.app.Activity

我追查了一下,DropInRequest 扩展了 Parcelable 而不是 Activity ...

最佳答案

要使用 pyjnius 创建上下文,您需要像这样转换它。

PythonActivity = autoclass('org.kivy.android.PythonActivity')
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
context = cast('android.content.Context', currentActivity.getApplicationContext())

关于java - 使用 pyjnius 在 python 中创建 Android Activity 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com