简体   繁体   English

如何在Qt for Android上获取应用程序参数

[英]How to get application arguments on Qt for android

I have a Qt application which has file associations defined in the AndroidManifest.xml so when I select a file in my browser I get a list of associated applications. 我有一个Qt应用程序,该应用程序在AndroidManifest.xml中定义了文件关联,因此当我在浏览器中选择文件时,会得到一个关联应用程序的列表。 My application is in the list, but when I select it the file path is not passed in the argv list in my main() method. 我的应用程序在列表中,但是当我选择它时,文件路径未在main()方法的argv列表中传递。 How is the path passed to the application and how can I have it in Qt/C++? 路径如何传递到应用程序,如何在Qt / C ++中使用它?

After some research I came out with the following working solution: 经过研究,我得出了以下可行的解决方案:

void loadAndroidFile()
{
#ifdef Q_OS_ANDROID
    QAndroidJniObject activity = QtAndroid::androidActivity();
    if (activity.isValid()) {
        QAndroidJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
        if (intent.isValid()) {
            QAndroidJniObject data = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
            if (data.isValid()) {
                QAndroidJniObject path = data.callObjectMethod("getPath", "()Ljava/lang/String;");
                if (path.isValid())
                    // Here path.toString() returns the path of the input file
                    QMetaObject::invokeMethod(rootComponent, "setSourcePath", Q_ARG(QVariant, QVariant("file://" + path.toString())));
            }
        }
    }
#endif
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM