QtAndroid Namespace

The QtAndroid namespace provides miscellaneous functions to aid Android development. More...

Header: #include <QtAndroid>
qmake: QT += androidextras
Since: Qt 5.3

Types

typedef Runnable

Functions

QAndroidJniObject androidActivity()
QAndroidJniObject androidContext()
int androidSdkVersion()
QAndroidJniObject androidService()
void hideSplashScreen()
void runOnAndroidThread(const Runnable &runnable)
void runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs = INT_MAX)
void startActivity(const QAndroidJniObject &intent, int receiverRequestCode, QAndroidActivityResultReceiver *resultReceiver = nullptr)
void startIntentSender(const QAndroidJniObject &intentSender, int receiverRequestCode, QAndroidActivityResultReceiver *resultReceiver = nullptr)

Detailed Description

The QtAndroid namespace provides miscellaneous functions to aid Android development.

Type Documentation

typedef QtAndroid::Runnable

Function Documentation

QAndroidJniObject QtAndroid::androidActivity()

Returns a handle to this application's main Activity

This function was introduced in Qt 5.3.

See also QAndroidJniObject, androidService(), and androidContext().

QAndroidJniObject QtAndroid::androidContext()

Returns a handle to this application's main Context. Depending on the nature of the application the Context object is either the main Service or Activity object.

This function was introduced in Qt 5.8.

See also QAndroidJniObject, androidActivity(), and androidService().

int QtAndroid::androidSdkVersion()

Returns the Android SDK version. This is also known as the API level.

This function was introduced in Qt 5.3.

QAndroidJniObject QtAndroid::androidService()

Returns a handle to this application's main Service

This function was introduced in Qt 5.7.

See also QAndroidJniObject, androidActivity(), and androidContext().

void QtAndroid::hideSplashScreen()

Hides the splash screen.

This function was introduced in Qt 5.7.

void QtAndroid::runOnAndroidThread(const Runnable &runnable)

Posts the given runnable on the android thread. The runnable will be queued and executed on the Android UI thread, unless it called on the Android UI thread, in which case the runnable will be executed immediately.

This function is useful to set asynchronously properties of objects that must be set on on Android UI thread.

This function was introduced in Qt 5.7.

void QtAndroid::runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs = INT_MAX)

Posts the runnable on the Android UI thread and waits until the runnable is executed, or until timeoutMs has passed

This function is useful to create objects, or get properties on Android UI thread:

QAndroidJniObject javaControl;
QtAndroid::runOnAndroidThreadSync([&javaControl](){

    // create our Java control on Android UI thread.
    javaControl = QAndroidJniObject("android/webkit/WebView",
                                                "(Landroid/content/Context;)V",
                                                QtAndroid::androidActivity().object<jobject>());
    javaControl.callMethod<void>("setWebViewClient",
                                   "(Landroid/webkit/WebViewClient;)V",
                                   QAndroidJniObject("android/webkit/WebViewClient").object());
});

// Continue the execution normally
qDebug() << javaControl.isValid();

This function was introduced in Qt 5.7.

void QtAndroid::startActivity(const QAndroidJniObject &intent, int receiverRequestCode, QAndroidActivityResultReceiver *resultReceiver = nullptr)

Starts the activity given by intent and provides the result asynchronously through the resultReceiver if this is non-null.

If resultReceiver is null, then the startActivity() method in the androidActivity() will be called. Otherwise startActivityForResult() will be called.

The receiverRequestCode is a request code unique to the resultReceiver, and will be returned along with the result, making it possible to use the same receiver for more than one intent.

This function was introduced in Qt 5.3.

void QtAndroid::startIntentSender(const QAndroidJniObject &intentSender, int receiverRequestCode, QAndroidActivityResultReceiver *resultReceiver = nullptr)

Starts the activity given by intentSender and provides the result asynchronously through the resultReceiver if this is non-null.

If resultReceiver is null, then the startIntentSender() method in the androidActivity() will be called. Otherwise startIntentSenderForResult() will be called.

The receiverRequestCode is a request code unique to the resultReceiver, and will be returned along with the result, making it possible to use the same receiver for more than one intent.

This function was introduced in Qt 5.3.

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