aboutsummaryrefslogtreecommitdiffstats
path: root/src/androidextras/android/qandroidfunctions.cpp
diff options
context:
space:
mode:
authorBogDan Vatra <bogdan@kdab.com>2016-02-04 17:16:05 +0200
committerBogDan Vatra <bogdan@kdab.com>2016-02-05 06:21:30 +0000
commitf0fefa8766cb3cec38f39c31bb01860bea88dcbc (patch)
tree98ba6d4a9156f68e29538eca9115df7a9d010573 /src/androidextras/android/qandroidfunctions.cpp
parent023d83792cd3f4cdffb9ae3a5464e4e8b654ddab (diff)
Helper functions needed to run Runnables on Android UI thread easily.
Add two function to allow the users to easily run (a)synchronously Runnables from any thread directly to Andoroid UI thread. These functions are useful to create java controls and to access their methods, which must done on Android UI thread. Change-Id: Iec5437321e6136cc90268cc7ecf091f82fc4cdd3 Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
Diffstat (limited to 'src/androidextras/android/qandroidfunctions.cpp')
-rw-r--r--src/androidextras/android/qandroidfunctions.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/androidextras/android/qandroidfunctions.cpp b/src/androidextras/android/qandroidfunctions.cpp
index a4d55c1..5c99642 100644
--- a/src/androidextras/android/qandroidfunctions.cpp
+++ b/src/androidextras/android/qandroidfunctions.cpp
@@ -41,6 +41,7 @@
#include "qandroidactivityresultreceiver.h"
#include "qandroidactivityresultreceiver_p.h"
+#include <QtCore/private/qjni_p.h>
#include <QtCore/private/qjnihelpers_p.h>
QT_BEGIN_NAMESPACE
@@ -151,4 +152,50 @@ void QtAndroid::startIntentSender(const QAndroidJniObject &intentSender,
}
+/*!
+ \since 5.7
+ \fn void QtAndroid::runOnAndroidThread(const Runnable &runnable)
+
+ Posts the given \a runnable on the android thread.
+ The \a 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.
+*/
+void QtAndroid::runOnAndroidThread(const QtAndroid::Runnable &runnable)
+{
+ QtAndroidPrivate::runOnAndroidThread(runnable, QJNIEnvironmentPrivate());
+}
+
+/*!
+ \since 5.7
+ \fn void QtAndroid::runOnAndroidThreadSync(const Runnable &runnable, int timeoutMs)
+
+ Posts the \a runnable on the Android UI thread and waits until the runnable is executed,
+ or until \a timeoutMs has passed
+
+ This function is useful to create objects, or get properties on Android UI thread:
+
+ \code
+ 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();
+ \endcode
+*/
+void QtAndroid::runOnAndroidThreadSync(const QtAndroid::Runnable &runnable, int timeoutMs)
+{
+ QtAndroidPrivate::runOnAndroidThreadSync(runnable, QJNIEnvironmentPrivate(), timeoutMs);
+}
+
QT_END_NAMESPACE