Qt on Android: Add sharing feature

Source: Internet
Author: User

A few days ago (2015-4-15) I saw an article from a foreigner about how to use QT on Android to share information with the social network, using the JNI features provided by QT, and the Qtandroid series of articles I wrote earlier.

The original is here: Sharing with Qt on Android. Is the article of December 12, 2014, hate not to meet when not married ... Sigh, it's nonsense again ... Or Google is powerful, you can search for good things.

The original author Zagge, translator Foruok (Http://blog.csdn.net/foruok), reproduced please indicate the source.

-------------------------------Split Line---------------------------------

We have just released Giraffpanic (a logic puzzle game developed with Qt & QML. Translator Note: A new version of Google play that can be downloaded to, for a fee). In this new release, we give the user the opportunity to share the unlock password (code) with each other to allow the user to unlock the new level (levels). We want to find a friendly way to share passwords between different devices, without having to copy and paste the password into another application. After trying a variety of ways (none of them work properly), we found that we could call the Android native share menu directly in our app. In this way, we keep our code neat and supportive of all the sharing methods provided by the host device.

Using that type of sharing, apps don't need any special permissions.

Here are the final results (images from the original English text):



You can download the sample code for the test app here (gitorious.org). To make it easier to understand, the code shown later is deliberately simplified.

Okay, what are we going to 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 Androidextras module to pro file

    ...    QT + = Androidextras ...    

Calling the Qt class of the Java class via 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, simplifying the use of JNI calls to Java methods. First we convert the QString object to the Java String object required by the Java method. We also pass the activity object that invokes the sharing intent (Intent) in the past. The Java method Sharetext is static, so we can use qandroidjniobject::callstaticmethod<void> () to invoke it.

The parameters of the Callstaticmethod method are as follows:

    • ClassName-Represents the class to which the Java method you want to call belongs, the fully qualified class name
    • MethodName-the name of the Java method you want to invoke
    • Signature of the Signature-java method
    • Parameters-all parameters to pass

Bogdan a good overview of Qt and JNI in Qt on Android episode 51. (Translator Note: can refer to my translation of the Chinese version of--QT on Android Episode 5 (translator)).

Make the Androidshare class available in QML

To make the Androidshare class visible in the QML environment, first we declare the Androidshare::share method as follows:

    q_invokable virtual void Share (const QString &title, const                                   QString &subject,                                   const QString &content );

We then add a Androidshare object to Qqmlcontext as a property 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, the calling code in QML is fairly straightforward.

I hope this article is helpful to the people you see.


If you want to see the effect of the code running in your game, you can download it to Google play. With the BlackBerry brothers, look at BlackBerry World ,N9 (Connaught's cell phone, is one of the original author's phone) powder can click here to download.

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

Here is the game, the translator Foruok gave:



It looks pretty good, too.


The blogger's Qtandroid series of articles is listed for reference:

    • Qtandroid detailed (5): JNI calls Android system features (2)
    • Qtandroid detailed (4): JNI calls Android system features (1)
    • Qtandroid detailed (3): StartActivity Android camera function
    • Qtandroid (2): StartActivity and its little friends
    • Qtandroid detailed (1): Qandroidjniobject
    • Qt on Android column

Qt on Android: Add sharing feature

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.