QNativeInterface::QAndroidApplication Struct

Android 核心應用程序的本地接口。 更多...

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

靜態公共成員

(從 6.2 起) QJniObject context ()
(從 6.2 起) void hideSplashScreen (int duration = 0)
(從 6.2 起) bool isActivityContext ()
(從 6.2 起) QFuture<QVariant> runOnAndroidMainThread (const std::function<QVariant ()> & runnable , const QDeadlineTimer timeout = QDeadlineTimer::Forever)
(從 6.2 起) int sdkVersion ()

詳細描述

訪問透過 QCoreApplication::nativeInterface()。

成員函數文檔編製

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

返迴 Android 上下文按 QtJniTypes::Context 。上下文是 Activity if the most recently started activity object is valid. Otherwise, the context is a Service .

該函數在 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 。若 duration is not provided (default is 0) the splash screen is hidden immedetiately after the app starts.

該函數在 Qt 6.2 引入。

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

返迴 true if QAndroidApplication::context () 提供 Activity 上下文。

該函數在 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 () 應該被使用。

runnable execution takes longer than the period of timeout , the blocking calls QFuture::waitForFinished () 和 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();
					

注意: 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 在您的 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 Keep your app responsive .

該函數在 Qt 6.2 引入。

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

返迴 Android SDK 版本。這又稱為 API 級彆。

該函數在 Qt 6.2 引入。