Auf dieser Seite

QNativeInterface::QAndroidApplication Struct

Native Schnittstelle zu einer Kernanwendung unter Android. Mehr...

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

Öffentliche Funktionen

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

Statische öffentliche Mitglieder

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

Ausführliche Beschreibung

Zugriff über QCoreApplication::nativeInterface().

Dokumentation der Mitgliedsfunktionen

[constexpr noexcept default] QAndroidApplication::QAndroidApplication()

Erzeugt standardmäßig eine Instanz von ` QAndroidApplication`.

[delete] QAndroidApplication::QAndroidApplication(const QNativeInterface::QAndroidApplication &)

Erstellt eine Instanz von „ QAndroidApplication “ durch Kopieren. Diese Funktion wird gelöscht.

[delete] QAndroidApplication::QAndroidApplication(QNativeInterface::QAndroidApplication &&)

Erstellt eine Instanz von ` QAndroidApplication`. Diese Funktion wurde gelöscht.

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

Gibt den Android-Kontext als ` QtJniTypes::Context` zurück. Der Kontext ist ein ` Activity `, wenn das zuletzt gestartete Aktivitätsobjekt gültig ist. Andernfalls ist der Kontext ein ` Service`.

Diese Funktion wurde in Qt 6.2 eingeführt.

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

Blendet den Begrüßungsbildschirm mithilfe eines Überblendungseffekts für den angegebenen Wert „ duration “ aus. Wenn „ duration “ nicht angegeben wird (Standardwert ist 0), wird der Begrüßungsbildschirm unmittelbar nach dem Start der App ausgeblendet.

Diese Funktion wurde in Qt 6.2 eingeführt.

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

Gibt „ true “ zurück, wenn „ QAndroidApplication::context()“ einen „ Activity “-Kontext liefert.

Diese Funktion wurde in Qt 6.2 eingeführt.

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

Übermittelt die Funktion „ runnable “ an den Android-Thread. Die Funktion wird in die Warteschlange gestellt und im Android-UI-Thread ausgeführt. Wenn der Aufruf im Android-UI-Thread erfolgt, wird „ runnable “ sofort ausgeführt. Wenn die Android-App angehalten ist oder die Haupt-Activity null ist, wird „ runnable “ in die Warteschlange des Android-Hauptthreads aufgenommen.

Dieser Aufruf gibt ein ` QFuture` <QVariant> zurück, das sowohl synchrone als auch asynchrone Aufrufe ermöglicht und jeden Rückgabetyp verarbeiten kann. Um jedoch ein Ergebnis von ` QFuture::result()` zurückzubekommen, sollte ` QVariant::value()` verwendet werden.

Wenn die Ausführung von ` runnable ` länger dauert als die Dauer von ` timeout`, werden die blockierenden Aufrufe von ` QFuture::waitForFinished()` und ` QFuture::result()` beendet, sobald ` timeout ` abgelaufen ist. Wenn jedoch die Ausführung von ` runnable ` bereits begonnen hat, wird sie nicht abgebrochen.

Das folgende Beispiel zeigt, wie ein asynchroner Aufruf ausgeführt wird, der einen Rückgabetyp erwartet:

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";
});

Das folgende Beispiel zeigt, wie ein synchroner Aufruf mit dem Rückgabetyp `void` ausgeführt wird:

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();

Hinweis: Achten Sie darauf, welche Operationen Sie im Hauptthread von Android ausführen, da jede lang andauernde Operation die Darstellung der Benutzeroberfläche und die Eingabeverarbeitung der App blockieren kann. Wenn die Funktion voraussichtlich eine lange Ausführungszeit hat, empfiehlt es sich außerdem, in Ihrem ` runnable ` einen ` QDeadlineTimer ` zu verwenden, um die Ausführung zu steuern und sicherzustellen, dass der UI-Thread nicht blockiert wird. In der Regel kann jeder Vorgang, der länger als 5 Sekunden dauert, die Benutzeroberfläche der App blockieren. Weitere Informationen finden Sie unter „Sorgen Sie dafür, dass Ihre App reaktionsschnell bleibt“.

Diese Funktion wurde in Qt 6.2 eingeführt.

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

Gibt die Version des Android SDK zurück. Diese wird auch als API-Level bezeichnet.

Diese Funktion wurde in Qt 6.2 eingeführt.

[delete] QNativeInterface::QAndroidApplication &QAndroidApplication::operator=(QNativeInterface::QAndroidApplication &&)

Weist „ other “ dieser Instanz von „ QAndroidApplication “ zu. Diese Funktion wird gelöscht.

[delete] QNativeInterface::QAndroidApplication &QAndroidApplication::operator=(const QNativeInterface::QAndroidApplication &)

other Weist der Instanz „ QAndroidApplication “ den Wert „ “ zu. Diese Funktion wird gelöscht.

© 2026 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.