Qt/QML Android best practice to send a custom Intent (share URL)

眉间皱痕 提交于 2019-12-05 00:37:09

问题


I was wondering if there are any best practice tips for sending custom android intents from QML (or c++ for that matter).

should I create a custom android activity and use the QAndroidJniObject class to call it or are there any better ways?

My intention is to create a simple share URL function from QML to other android apps.

thanks


回答1:


Extend QtActivity with additional static method:

package org.whatever

public class YourActivity extends org.qtproject.qt5.android.bindings.QtActivity
{
    private static YourActivity instance;

    YourActivity() {
        instance = this;
    }

    public static void shareUrl(QString url) {
        //create intent here
        //can use instance object
    }
}

On c++ side call shareUrl method using QAndroidJniObject

class QmlInterface : public QObject
{
    Q_OBJECT
    public:
        QmlInterface();
        Q_INVOKABLE void shareUrl( QString url );
};

and implementation:

void QmlInterface:: shareUrl( QString url )
{
#ifdef Q_OS_ANDROID
    QAndroidJniObject::callStaticMethod( "org/whatever/YourActivity",
                                         "shareUrl",
                                         "(Ljava/lang/String;)V",
                                         QAndroidJniObject::fromString( url ));
#endif
}

Using static method on java side simplifies jni call significantly because you don't have to get Activity instance. Because Activity context is needed to send Intent static instance member object is used on java side.



来源:https://stackoverflow.com/questions/22672522/qt-qml-android-best-practice-to-send-a-custom-intent-share-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!