QtAndroid Namespace

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

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

Types

enum BindFlag { None, AutoCreate, DebugUnbind, NotForeground, ..., ExternalService }
flags BindFlags
enum PermissionResult { Granted, Denied }
typedef PermissionResultCallback
typedef PermissionResultMap
typedef Runnable

Functions

QAndroidJniObject androidActivity()
QAndroidJniObject androidContext()
int androidSdkVersion()
QAndroidJniObject androidService()
bool bindService(const QAndroidIntent &serviceIntent, const QAndroidServiceConnection &serviceConnection, BindFlags flags = BindFlag::None)
PermissionResult checkPermission(const QString &permission)
void hideSplashScreen()
void hideSplashScreen(int duration)
void requestPermissions(const QStringList &permissions, const PermissionResultCallback &callbackFunc)
PermissionResultMap requestPermissionsSync(const QStringList &permissions, int timeoutMs = INT_MAX)
void runOnAndroidThread(const Runnable &runnable)
void runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs = INT_MAX)
bool shouldShowRequestPermissionRationale(const QString &permission)
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

enum QtAndroid::BindFlag
flags QtAndroid::BindFlags

This enum is used with QtAndroid::bindService to describe the mode in which the binding is performed.

ConstantValueDescription
QtAndroid::None0x00000000No options.
QtAndroid::AutoCreate0x00000001Automatically creates the service as long as the binding exist. See BIND_AUTO_CREATE documentation for more details.
QtAndroid::DebugUnbind0x00000002Include debugging help for mismatched calls to unbind. See BIND_DEBUG_UNBIND documentation for more details.
QtAndroid::NotForeground0x00000004Don't allow this binding to raise the target service's process to the foreground scheduling priority. See BIND_NOT_FOREGROUND documentation for more details.
QtAndroid::AboveClient0x00000008Indicates that the client application binding to this service considers the service to be more important than the app itself. See BIND_ABOVE_CLIENT documentation for more details.
QtAndroid::AllowOomManagement0x00000010Allow the process hosting the bound service to go through its normal memory management. See BIND_ALLOW_OOM_MANAGEMENT documentation for more details.
QtAndroid::WaivePriority0x00000020Don't impact the scheduling or memory management priority of the target service's hosting process. See BIND_WAIVE_PRIORITY documentation for more details.
QtAndroid::Important0x00000040This service is assigned a higher priority so that it is available to the client when needed. See BIND_IMPORTANT documentation for more details.
QtAndroid::AdjustWithActivity0x00000080If binding from an activity, allow the target service's process importance to be raised based on whether the activity is visible to the user. See BIND_ADJUST_WITH_ACTIVITY documentation for more details.
QtAndroid::ExternalService-2147483648The service being bound is an isolated, external service. See BIND_EXTERNAL_SERVICE documentation for more details.

This enum was introduced or modified in Qt 5.10.

The BindFlags type is a typedef for QFlags<BindFlag>. It stores an OR combination of BindFlag values.

enum QtAndroid::PermissionResult

This enum is used to describe the permission status.

ConstantValueDescription
QtAndroid::Granted0The permission was granted.
QtAndroid::Denied1The permission was denied.

This enum was introduced or modified in Qt 5.10.

typedef QtAndroid::PermissionResultCallback

Synonym for std::function<void(const PermissionResultMap &)>.

typedef QtAndroid::PermissionResultMap

Synonym for QHash<QString, PermissionResult>.

typedef QtAndroid::Runnable

Synonym for std::function<void()>.

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

bool QtAndroid::bindService(const QAndroidIntent &serviceIntent, const QAndroidServiceConnection &serviceConnection, BindFlags flags = BindFlag::None)

Binds the service given by serviceIntent, serviceConnection and flags. The serviceIntent object identifies the service to connect to. The serviceConnection is a listener that receives the information as the service is started and stopped.

Returns true on success

See Android documentation documentation for more details.

This function was introduced in Qt 5.10.

See also QAndroidIntent, QAndroidServiceConnection, and BindFlag.

PermissionResult QtAndroid::checkPermission(const QString &permission)

Checks if the permission was granted or not. This function should be called every time when the application starts for needed permissions, as the users might disable them from Android Settings.

This function was introduced in Qt 5.10.

void QtAndroid::hideSplashScreen()

Hides the splash screen immediately.

This function was introduced in Qt 5.7.

void QtAndroid::hideSplashScreen(int duration)

Hides the splash screen, fading it for duration milliseconds.

This function was introduced in Qt 5.10.

void QtAndroid::requestPermissions(const QStringList &permissions, const PermissionResultCallback &callbackFunc)

Asynchronously requests permissions to be granted to this application, callbackFunc will be called with the results.

This function was introduced in Qt 5.10.

PermissionResultMap QtAndroid::requestPermissionsSync(const QStringList &permissions, int timeoutMs = INT_MAX)

Synchronously requests permissions to be granted to this application, waits timeoutMs to complete.

This function was introduced in Qt 5.10.

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.

bool QtAndroid::shouldShowRequestPermissionRationale(const QString &permission)

Returns true if you should show UI with a rationale for requesting a permission.

This function was introduced in Qt 5.10.

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.

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