Android 核心应用程序的本地接口。 更多...
头: | #include < QCoreApplication > |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 6.2 |
int | context () |
void | hideSplashScreen (int duration = 0) |
bool | isActivityContext () |
QFuture<QVariant> | runOnAndroidMainThread (const std::function<QVariant ()> & runnable , const QDeadlineTimer timeout = QDeadlineTimer::Forever) |
int | sdkVersion () |
访问透过 QCoreApplication::nativeInterface()。
[static, since 6.2]
int
QAndroidApplication::
context
()
返回 Android 上下文按
jobject
。上下文是
Activity
若主活动对象有效。否则,上下文是
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 Keeping your app responsive .
该函数在 Qt 6.2 引入。
[static, since 6.2]
int
QAndroidApplication::
sdkVersion
()
返回 Android SDK 版本。这又称为 API 级别。
该函数在 Qt 6.2 引入。