QAndroidApplication Struct

struct QNativeInterface::QAndroidApplication

Native interface to a core application on Android. More...

Header: #include <QCoreApplication>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Since: Qt 6.2

Public Functions

QAndroidApplication(QNativeInterface::QAndroidApplication &&)
QAndroidApplication(const QNativeInterface::QAndroidApplication &)
QNativeInterface::QAndroidApplication &operator=(QNativeInterface::QAndroidApplication &&)
QNativeInterface::QAndroidApplication &operator=(const QNativeInterface::QAndroidApplication &)

Static Public Members

jobject context()
void hideSplashScreen(int duration = 0)
bool isActivityContext()
QFuture<QVariant> runOnAndroidMainThread(const std::function<QVariant ()> &runnable, const QDeadlineTimer timeout = QDeadlineTimer::Forever)
std::enable_if_t<std::is_invocable_v<T> && std::is_same_v<std::invoke_result_t<T>, void>, QFuture<void>> runOnAndroidMainThread(const T &runnable, const QDeadlineTimer timeout = QDeadlineTimer::Forever)
int sdkVersion()

Protected Functions

Detailed Description

Accessed through QCoreApplication::nativeInterface().

Member Function Documentation

QAndroidApplication::QAndroidApplication(QNativeInterface::QAndroidApplication &&)

Default constructs an instance of QAndroidApplication.

QAndroidApplication::QAndroidApplication(const QNativeInterface::QAndroidApplication &)

Default constructs an instance of QAndroidApplication.


Default constructs an instance of QAndroidApplication.

QNativeInterface::QAndroidApplication &QAndroidApplication::operator=(QNativeInterface::QAndroidApplication &&)

Move-assignment operator.

QNativeInterface::QAndroidApplication &QAndroidApplication::operator=(const QNativeInterface::QAndroidApplication &)

Copy-assignment operator.

[virtual protected] QAndroidApplication::~QAndroidApplication()

Destroys the instance of QAndroidApplication. The destructor is virtual.

[static, since 6.2] jobject QAndroidApplication::context()

Returns the Android context as a jobject. The context is an Activity if the main activity object is valid. Otherwise, the context is a Service.

This function was introduced in Qt 6.2.

[static, since 6.2] void QAndroidApplication::hideSplashScreen(int duration = 0)

Hides the splash screen by using a fade effect for the given duration. If duration is not provided (default is 0) the splash screen is hidden immedetiately after the app starts.

This function was introduced in Qt 6.2.

[static, since 6.2] bool QAndroidApplication::isActivityContext()

Returns true if QAndroidApplication::context() provides an Activity context.

This function was introduced in Qt 6.2.

[static, since 6.2] QFuture<QVariant> QAndroidApplication::runOnAndroidMainThread(const std::function<QVariant ()> &runnable, const QDeadlineTimer timeout = QDeadlineTimer::Forever)

Posts the function runnable to the Android thread. The function will be queued and executed on the Android UI thread. If the call is made on the Android UI thread runnable will be executed immediately. If the Android app is paused or the main Activity is null, runnable is added to the Android main thread's queue.

This call returns a QFuture<QVariant> which allows doing both synchronous and asynchronous calls, and can handle any return type. However, to get a result back from the QFuture::result(), QVariant::value() should be used.

If the runnable execution takes longer than the period of timeout, the blocking calls QFuture::waitForFinished() and QFuture::result() are ended once timeout has elapsed. However, if runnable has already started execution, it won't be cancelled.

The following example shows how to run an asynchronous call that expects a return type:

auto task = QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
    QJniObject surfaceView;
    if (!surfaceView.isValid())
        qDebug() << "SurfaceView object is not valid yet";

    surfaceView = QJniObject("android/view/SurfaceView",

    return QVariant::fromValue(surfaceView);
}).then([](QFuture<QVariant> future) {
    auto surfaceView = future.result().value<QJniObject>();
    if (surfaceView.isValid())
        qDebug() << "Retrieved SurfaceView object is valid";

The following example shows how to run a synchronous call with a void return type:

QNativeInterface::QAndroidApplication::runOnAndroidMainThread([]() {
   QJniObject activity = QNativeInterface::QAndroidApplication::context();
   // Hide system ui elements or go full screen
   activity.callObjectMethod("getWindow", "()Landroid/view/Window;")
           .callObjectMethod("getDecorView", "()Landroid/view/View;")
           .callMethod<void>("setSystemUiVisibility", "(I)V", 0xffffffff);

Note: Becareful about the type of operations you do on the Android's main thread, as any long operation can block the app's UI rendering and input handling. If the function is expected to have long execution time, it's also good to use a QDeadlineTimer in your runnable to manage the execution and make sure it doesn't block the UI thread. Usually, any operation longer than 5 seconds might block the app's UI. For more information, see Keeping your app responsive.

This function was introduced in Qt 6.2.

[static] template <typename T> std::enable_if_t<std::is_invocable_v<T> && std::is_same_v<std::invoke_result_t<T>, void>, QFuture<void>> QAndroidApplication::runOnAndroidMainThread(const T &runnable, const QDeadlineTimer timeout = QDeadlineTimer::Forever)

[static, since 6.2] int QAndroidApplication::sdkVersion()

Returns the Android SDK version. This is also known as the API level.

This function was introduced in Qt 6.2.

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.