aboutsummaryrefslogtreecommitdiffstats
path: root/examples/androidextras/jnimessenger/doc/src/qtandroidextras-example-jnimessenger.qdoc
blob: 7368bcdd14951a37a747e71865da69ac955a72b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtAndroidExtras module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

/*!
    \title Qt JNI Messenger
    \ingroup examples-qtandroidextras
    \example jnimessenger
    \brief Demonstrates communication between Java code and QML or C++ using NJI calls.

    \image jnimessenger.png

    This example demonstrates how to add a custom Java class to an Android application, and
    how to both call it from C++ and call C++ functions from Java using the JNI convenience
    APIs in the Qt Android Extras module. The application UI is created by using Qt Quick.

    When clicking the send button, a message will be sent from QML to Java class though
    the C++ class and a log of that is shown in the screen view. Logs also can be seen
    from the Android logcat of the messages being exchanged, which would be similar to:

    \badcode
        I System.out: This is printed from JAVA, message is: QML sending to Java: Hello from QML
        D libjnimessenger_armeabi-v7a.so: qml: QML received a message: Hello from JAVA!
    \endcode

    \include examples-run.qdocinc

    \section1 Calling Java Methods from C++ Code

    We define a custom Java class called \c JniMessenger in the JniMessenger.java file:

    \quotefromfile jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java
    \skipto org.qtproject.example.jnimessenger
    \printuntil /^\}/

    \note The custom Java class can extend other classes like QtActivity, Activity or any
    other Java class.

    In the jnimessenger.cpp file, we call the function \c printFromJava(String message)
    by first creating a \c QAndroidJniObject for the Java String that we want to send
    and then invoking a JNI call with \c callStaticMethod<>() while providing the method
    signature:

    \quotefromfile jnimessenger/jnimessenger.cpp
    \skipto void JniMessenger::printFromJava
    \printuntil }

    That call will then execute the following from Java side, which would print the
    message to the \c System.output.

    \quotefromfile jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java
    \skipto public static void printFromJava
    \printuntil );

    \section1 Calling QML/C++ Functions from Java Code

    Directly after that, our native function \c callFromJava(String message) will be
    called, which would be then handled from C++ side. Note, that this method has
    to be defined as \c native at the top of the Java class as:

    \code
        private static native void callFromJava(String message);
    \endcode

    To be able to call C++ functions from Java, in our C++ class JniMessenger.cpp,
    we need to define those functions using \c RegisterNatives() as follows:

    \quotefromfile jnimessenger/jnimessenger.cpp
    \skipto JNINativeMethod
    \printuntil }

    (See \l{Java Native Methods} for more details).

    We would need to register the functions' signatures in \c methods[], which have
    the name in Java class, then its parameters and return types, then the function
    pointer in the C++ code.

    \code
        JNINativeMethod methods[] {{"callFromJava", "(Ljava/lang/String;)V", reinterpret_cast<void *>(callFromJava)}};
    \endcode

    This would insure that our C++ function is available from within the Java call.
    Now, that function could simply print the message it received from Java to
    the debug log, but we want to forward the received message to the QML components
    so that it gets displayed in our text view, so we get:

    \quotefromfile jnimessenger/jnimessenger.cpp
    \skipto static void callFromJava
    \printuntil }

    Now, we need to implement the necessary \c Connections in the QML code to receive
    the message from C++, which we would print into the \c Text view with the id \c messengerLog:

    \quotefromfile jnimessenger/main.qml
    \skipto Connections
    \printuntil /^\ {4}\}/

    \sa {Qt for Android}, {Qt Android Extras}
*/