6

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 Answer 1

6

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.

1
  • thanks for the solution. I have one question though: does the activity has to extend QtActivity for any specific reason or can a default Activity also be used since it just calls the static method via JNI?
    – Xander
    Jun 11, 2014 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.