QAndroidApplication Struct

struct QNativeInterface::QAndroidApplication

Androidのコア・アプリケーションのネイティブ・インターフェース。詳細...

ヘッダー #include <QCoreApplication>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
以来:Qt 6.2

静的パブリックメンバー

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

詳しい説明

QCoreApplication::nativeInterface() からアクセスします。

メンバー関数ドキュメント

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

AndroidのコンテキストをQJniObject 。 直近に開始したアクティビティオブジェクトが有効な場合、コンテキストはActivity 。そうでない場合、コンテキストはService です。

この関数はQt 6.2で導入されました。

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

与えられたduration に対してフェード効果を使用してスプラッシュ画面を隠します。duration が提供されない場合(デフォルトは 0)、スプラッシュスクリーンはアプリの開始直後に非表示になります。

この関数は Qt 6.2 で導入されました。

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

QAndroidApplication::context() がActivity コンテキストを提供する場合、true を返します。

この関数は Qt 6.2 で導入されました。

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

Android スレッドに関数runnable をポストする。この関数はキューに入れられ、Android UIスレッドで実行される。呼び出しがAndroid UIスレッドで行われた場合、runnable は直ちに実行されます。Androidアプリが一時停止されているか、メイン・アクティビティがNULLの場合、runnable 、Androidメイン・スレッドのキューに追加されます。

この呼び出しは、QFuture<QVariant>を返すので、同期呼び出しと非同期呼び出しの両方が可能で、どんな戻り値の型も扱える。ただし、QFuture::result ()から結果を返すには、QVariant::value ()を使用する必要がある。

runnable の実行がtimeout の期間よりも長い場合、QFuture::waitForFinished() とQFuture::result() のブロッキング呼び出しは、timeout が経過した時点で終了する。ただし、runnable がすでに実行を開始している場合は、キャンセルされない。

以下の例では、戻り値を期待する非同期呼び出しの実行方法を示している:

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

    surfaceView=QJniObject("android/view/SurfaceView", "(Landroid/content/Context;)V", QNativeInterface::QAndroidApplication::context());returnQVariant::fromValue(surfaceView); }).then([](QFuture<QVariant>future) {autosurfaceView=future.result().value<QJniObject>();if(surfaceView.isValid())        qDebug() << "Retrieved SurfaceView object is valid";
});

次の例は、戻り値がvoid型の同期呼び出しの実行方法を示している:

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

注意: Androidのメインスレッドで行う操作の種類には注意してください。長い操作はアプリのUIレンダリングや入力処理をブロックする可能性があります。関数の実行時間が長くなることが予想される場合は、runnableQDeadlineTimer を使用して実行を管理し、UIスレッドをブロックしないようにするのもよいでしょう。通常、5秒以上の操作はアプリのUIをブロックする可能性があります。詳しくは、Keeping your app responsiveを参照してください。

この関数は Qt 6.2 で導入されました。

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

Android SDKのバージョンを返します。これはAPIレベルとしても知られています。

この関数は Qt 6.2 で導入されました。

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