Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top