Sharing with Qt on Android

We just release a new version of GiraffPanic – a logic mobile game written with Qt and QML. In the new version we give the users the possibility to share unlock codes with each other to unlock new levels. So we wanted to have a nice way to share the code between devices without any need to copy paste them (codes) into another application. After trying a lot of different approaches (that did not work), we found it is possible to invoke the native Android share menu from within our application. Using this method keeps our own code quite tidy and supports all the ways of sharing provided by the host device.

By using sharing that way the application does not need any special permissions.

Here is a preview of what the end result might look like:

GiraffPanic Share Android Share Share Message

You can find sample source code for the test app here. The code below is shortened to make it easier to understand.

So what is needed for this to work?

1. Java class that calls the native Android Java API

    public class ShareIntent
        static public void shareText(String title, String subject, 
                                     String content, QtActivity activity)
            Intent share = new Intent(Intent.ACTION_SEND);

            share.putExtra(Intent.EXTRA_SUBJECT, subject);
            share.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(content).toString());
            share.putExtra(Intent.EXTRA_HTML_TEXT, content);
            activity.startActivity(Intent.createChooser(share, title));

 2. The androidextras module in .pro file

    QT += androidextras

3. Qt class to call Java class via JNI(Java Native Interface)

    void QtAndroidShare::share(const QString &title, const QString &subject, const QString &content)
        QAndroidJniObject jTitle = QAndroidJniObject::fromString(title);
        QAndroidJniObject jSubject = QAndroidJniObject::fromString(subject);
        QAndroidJniObject jContent = QAndroidJniObject::fromString(content);
        QAndroidJniObject activity = QtAndroid::androidActivity();

The QAndroidJniObject class is part of the andoridextras module and simplify the calling of Java methods with JNI. First we convert the QString objects to a Java String objects which are used as parameters for the Java method. We also pass the activity object to start the share intent from. As the Java method shareText is static public void we can use QAndroidJniObject::callStaticMethod<void>() to call it.
The parameters are:

    className  - This reflects the path where the class is at
    methodName - The method we want to call
    signature  - The signature of the method we want to call. In our case the function takes
                 3 String parameters and one QtActivity parameter and returns void.
    parameters - All the parameters passed to the method.

In the article Qt on Android Episode 5 Bogdan gives a nice Overview about the Qt and JNI.

4. Make the AndroidShare class available from QML

To make the AndroidShare class available from QML AndroidShare::share is declared as

    Q_INVOKABLE virtual void share(const QString &title,
                                   const QString &subject,
                                   const QString &content);

and an object of the class is added to the QQmlContext

    QQmlApplicationEngine engine;
    QQmlContext *context = engine.rootContext();

    qmlRegisterType<QtAndroidShare>("QtAndroidShare", 1, 0, "ShareIntent");
    context->setContextProperty("shareIntent", new QtAndroidShare());

5. Use from QML

    Button {
        text: "Press to share"
        onClicked: {
            shareIntent.share(title.text, subject.text, content.text);

As you can see calling the code from QML is now quite easy. I hope this helps some people out there.

To see the code in action in our game download it for free from Google Play. It is also available on BlackBerry World and there is a download of the game for the N9 here (it is no longer possible to upload anything to the store) as that is the phone I still use on a daily basis.

This entry was posted in Blogs, Thorsten Zachmann. Bookmark the permalink.

2 Responses to Sharing with Qt on Android

  1. Dave says:

    Thank you for posting this!

  2. Damian Ivanov says:

    Thanks a lot. Saved me a lot lot of work…