IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Detailed Description

Native interface to a core application on Android.

This struct was introduced in Qt 6.2.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

  • Header: QCoreApplication

  • Since: Qt 6.2

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Group: QAndroidApplication is part of native-interfaces, native-interfaces-qcoreapplication

Detailed Description

Accessed through QCoreApplication::nativeInterface().

Member Function Documentation

 

[static, since 6.2] int 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:

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

    surfaceView = QJniObject("android/view/SurfaceView",
                             "(Landroid/content/Context;)V",
                             QNativeInterface::QAndroidApplication::context());

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

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

 
Sélectionnez
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&lt;void&gt;("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 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.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+