Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 857
Xander Avatar asked Mar 26 '14 20:03

Xander


1 Answers

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.

like image 161
lkorbel Avatar answered Oct 19 '22 20:10

lkorbel