From ec3c96d72d17538406fe77274ba0b5ceed680254 Mon Sep 17 00:00:00 2001 From: Petri Virkkunen Date: Thu, 15 Feb 2024 11:57:33 +0200 Subject: [PATCH] Android: PlatformServices:Do not expect Intents in non-Activity usecase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In non-Activity contexts, do not register the QPlatformServices class as an intent listener, as only Qt applications based on Qt Activities will receive new Intents. Calling getIntent on a non-Activity context will also cause an error, as getIntent is a method of Activity, not Context. Skip both when constructing QPlatformServices for applications without an Activity context. Task-number: QTBUG-118874 Change-Id: Ide64bd6a90d8db0a7654968ff42cdaa5da1d3b51 Reviewed-by: Assam Boudjelthia Reviewed-by: Tinja Paavoseppä --- .../android/qandroidplatformservices.cpp | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/plugins/platforms/android/qandroidplatformservices.cpp b/src/plugins/platforms/android/qandroidplatformservices.cpp index f43e7cdd6a..39287aa905 100644 --- a/src/plugins/platforms/android/qandroidplatformservices.cpp +++ b/src/plugins/platforms/android/qandroidplatformservices.cpp @@ -24,15 +24,18 @@ QAndroidPlatformServices::QAndroidPlatformServices() QtAndroidPrivate::registerNewIntentListener(this); - QMetaObject::invokeMethod( - this, - [this] { - QJniObject context = QJniObject(QtAndroidPrivate::context()); - QJniObject intent = - context.callObjectMethod("getIntent", "()Landroid/content/Intent;"); - handleNewIntent(nullptr, intent.object()); - }, - Qt::QueuedConnection); + // Qt applications without Activity contexts cannot retrieve intents from the Activity. + if (QNativeInterface::QAndroidApplication::isActivityContext()) { + QMetaObject::invokeMethod( + this, + [this] { + QJniObject context = QJniObject(QtAndroidPrivate::context()); + QJniObject intent = + context.callObjectMethod("getIntent", "()Landroid/content/Intent;"); + handleNewIntent(nullptr, intent.object()); + }, + Qt::QueuedConnection); + } } Q_DECLARE_JNI_CLASS(UriType, "android/net/Uri")