Qt on Android: added the sharing function, qtandroid

Source: Internet
Author: User

Qt on Android: added the sharing function, qtandroid

A few days ago () I saw an article by a foreigner about how to use Qt on Android to share information with social networks and implement it using the JNI function provided by Qt, I have translated this article with the QtAndroid detailed series I have previously written.

Original article: Sharing with Qt on Android. It's an article in December 12, 2014. Hate it when you are not married ...... Sigh, it's nonsense again ...... Google is powerful, so you can search for good things.

Original Author zagge, translator foruok (http://blog.csdn.net/foruok), reprint please indicate the source.

------------------------------- Split line ---------------------------------

We have just released GiraffPanic (a logical puzzle game developed with Qt & QML. Note: a new version of Google Play can be downloaded and charged. In this new version, we provide users with the opportunity to share their unlock passwords (codes) so that users can easily unlock new levels (levels ). We want to find a friendly way to share passwords between different devices. We do not need to copy and paste the passwords into another application. After trying many ways (none of them work normally), we found that we can directly call the sharing menu of Android native in our applications. This method not only keeps our code clean, but also supports all the sharing methods provided by the host device.

In this way, the application does not require any special permissions.

The final effect is shown below (the picture is from the original English text ):



You can download the sample code of the test application from here (gitorious.org. For easier understanding, the code displayed later is specially simplified.

Well, what do we do?

Java class that calls Android native API

    ...    public class ShareIntent    {        static public void shareText(String title, String subject,                                      String content, QtActivity activity)        {            Intent share = new Intent(Intent.ACTION_SEND);            share.setType("text/plain");            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));        }    }    ...

Add the androidextras module to the pro File

    ...    QT += androidextras    ...

Call the Java class Qt class through JNI

    ...    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();        QAndroidJniObject::callStaticMethod<void>(            "net/exit0/androidshare/ShareIntent",            "shareText",            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;"                  "Lorg/qtproject/qt5/android/bindings/QtActivity;)V",            jTitle.object<jstring>(),            jSubject.object<jstring>(),            jContent.object<jstring>(),            activity.object<jobject>()       );    }

QAndroidJniObject is part of the andoridextras module provided by Qt, which simplifies the use of JNI to call Java methods. First, convert the QString object to the Java String object required by the Java method. At the same time, we also pass the Activity object that calls the sharing Intent over. The Java method shareText is static, so we can use QAndroidJniObject: callStaticMethod <void> () to call it.

The parameters of the callStaticMethod method are as follows:

  • ClassName-indicates the class to which the Java method you want to call belongs. The fully qualified class name
  • MethodName-name of the Java method you want to call
  • Signature-Java method signature
  • Parameters-all parameters to be passed

Bogdan gave a good overview of Qt and JNI in Qt on Android Episode 5. (For more information, see Qt on Android Episode 5 )).

Make the AndroidShare class available in QML

To make the AndroidShare class visible in the QML environment, we declare the AndroidShare: share Method as follows:

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

Then we add an AndroidShare object to the QQmlContext as an attribute of the QML context:

    ...    QQmlApplicationEngine engine;    QQmlContext *context = engine.rootContext();    qmlRegisterType<QtAndroidShare>("QtAndroidShare", 1, 0, "ShareIntent");    context->setContextProperty("shareIntent", new QtAndroidShare());    ...

Use in QML

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

As you can see, calling code in QML is quite simple.

I hope this article will help you.


If you want to see how the code works in your game, you can download it on Google Play. You can click here to download the powder from BlackBerry's brother, watch BlackBerry World and ghost (one of the phones that the original author is still using.

Note: The benefits of Qt cross-platform are ......

The following is what the translator foruok gave the game:



It looks pretty good.


The topic's QtAndroid series are listed for reference:

  • QtAndroid (5): JNI calls Android system functions (2)
  • QtAndroid (4): JNI calls Android system functions (1)
  • Details on QtAndroid (3): startActivity for Android photography
  • QtAndroid (2): startActivity and its friends
  • QtAndroid (1): QAndroidJniObject
  • Qt on Android Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.