- Qt 6.6
- Qt Core
- C++ Classes
- QAndroidApplication
- Qt 6.6.3 Reference Documentation

# 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

- List of all members, including inherited members
- QAndroidApplication is part of Native Interfaces.

## 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)
int | sdkVersion()

## Detailed Description

Accessed through QCoreApplication::nativeInterface().

## Member Function Documentation

### `[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",
                                  "(Landroid/content/Context;)V",
                                  QNativeInterface::QAndroidApplication::context());

         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);
     }).waitForFinished();

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, 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.