Qt QAndroidActivityResultReceiver handleActivityResult()尚未从startActivityForResult调用的活动收到任何应答

jei2mxaa  于 2023-03-21  发布在  Android
关注(0)|答案(1)|浏览(131)

函数CallActivity::runApplication通常在我按下被调用Activity中的按钮时通过startActivityForResult启动其他应用的Activity,并通过setResult(RESULT_OK,intent)将结果返回给调用应用;函数CallActivity::handleActivityResult必须已收到应答,但未返回任何内容。当我从Android Studio应用调用应用时,我正常收到应答。handleActivityResult有什么问题?如何通过handleActivityResult在Qt应用中接收应答?Android 7.1,Qt 5.15.2
callactivity.h类继承自类CallActivity:公共QObject、公共QAndroid活动结果接收器!!!

class CallActivity : public QObject, public QAndroidActivityResultReceiver
{
    Q_OBJECT
public:
    explicit CallActivity(QObject *parent = nullptr);
    Q_INVOKABLE void runApplication(const QString &packageName, const QString &className);
    void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)  override;
};

callactivity.cpp

void CallActivity::runApplication(const QString &packageName, const QString &className)
{
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod(
                "org/qtproject/qt5/android/QtNative", "activity",
                "()Landroid/app/Activity;");   
    QAndroidJniObject jPackageName = QAndroidJniObject::fromString(packageName);
    QAndroidJniObject jClassName = QAndroidJniObject::fromString(className);
    QAndroidJniObject intent("android/content/Intent","()V");
    intent.callObjectMethod("setClassName",
                            "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
                            jPackageName.object<jstring>(),jClassName.object<jstring>());
    QAndroidJniEnvironment env;
    activity.callMethod<void>(
                "startActivityForResult",
                "(Landroid/content/Intent;I)V",
                intent.object<jobject>(),1);
}
void CallActivity::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) //isn't called!!!!
{
    qDebug()<<"handleActivityResult"<<receiverRequestCode<<resultCode; //!!!!!RECEIVED NOTHING
}

main.qml

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    Button {
        onClicked: {
            clcallActivity.runApplication("com.my.startactivityforres",
                                           "com.my.startactivityforres.ActivityDate")
        }
    }
}
xuo3flqw

xuo3flqw1#

我的错误是单独使用QAndroidActivityResultReceiver,但有必要将其指针传递到QtAndroid::startActivity
void QtAndroid::开始活动(常量QAndroid意图和意图,整数接收器请求代码,QAndroid活动结果接收器 * 结果接收器=空指针)

相关问题