diff options
Diffstat (limited to 'examples/corelib/platform/androidnotifier')
6 files changed, 37 insertions, 30 deletions
diff --git a/examples/corelib/platform/androidnotifier/CMakeLists.txt b/examples/corelib/platform/androidnotifier/CMakeLists.txt index e5271edd79..b356b15a43 100644 --- a/examples/corelib/platform/androidnotifier/CMakeLists.txt +++ b/examples/corelib/platform/androidnotifier/CMakeLists.txt @@ -1,5 +1,5 @@ # Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(androidnotifier LANGUAGES CXX) @@ -8,16 +8,10 @@ if(NOT ANDROID) message(FATAL_ERROR "Example only works on Android") endif() -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - find_package(Qt6 REQUIRED COMPONENTS Widgets) qt_standard_project_setup() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/platform/androidnotifier") - qt_add_executable(androidnotifier MANUAL_FINALIZATION main.cpp @@ -28,6 +22,7 @@ qt_add_executable(androidnotifier ) target_link_libraries(androidnotifier PRIVATE + Qt6::CorePrivate Qt6::Widgets ) @@ -49,7 +44,14 @@ qt_add_resources(androidnotifier "main" ) install(TARGETS androidnotifier - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET androidnotifier + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml b/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml index 4850a8e8e6..1d3cf1325b 100644 --- a/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml +++ b/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml @@ -4,12 +4,9 @@ android:installLocation="auto" android:versionCode="1" android:versionName="1.0"> - <!-- The comment below will be replaced with dependencies permissions upon deployment. - Remove the comment if you do not require these default permissions. --> - <!-- %%INSERT_PERMISSIONS --> - <!-- The comment below will be replaced with dependencies permissions upon deployment. - Remove the comment if you do not require these default features. --> + <!-- %%INSERT_PERMISSIONS --> + <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <!-- %%INSERT_FEATURES --> <supports-screens @@ -23,13 +20,15 @@ android:hardwareAccelerated="true" android:label="Qt Notifier" android:requestLegacyExternalStorage="true" - android:icon="@drawable/icon"> + android:icon="@drawable/icon" + android:allowBackup="true" + android:fullBackupOnly="false"> <activity android:name="org.qtproject.qt.android.bindings.QtActivity" android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density" - android:label="Qt Notifier" android:launchMode="singleTop" - android:screenOrientation="unspecified"> + android:screenOrientation="unspecified" + android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> @@ -38,14 +37,6 @@ <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/> - - <meta-data - android:name="android.app.background_running" - android:value="false"/> - - <meta-data - android:name="android.app.extract_android_style" - android:value="none" /> </activity> </application> </manifest> diff --git a/examples/corelib/platform/androidnotifier/androidnotifier.pro b/examples/corelib/platform/androidnotifier/androidnotifier.pro index f1650c1911..7e5b845e10 100644 --- a/examples/corelib/platform/androidnotifier/androidnotifier.pro +++ b/examples/corelib/platform/androidnotifier/androidnotifier.pro @@ -1,5 +1,5 @@ -QT += core gui -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +QT += core gui widgets +QT += core-private # For Notification permission request SOURCES += \ main.cpp \ diff --git a/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc b/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc index 4bc0f7668e..1315ad7762 100644 --- a/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc +++ b/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc @@ -4,7 +4,9 @@ /*! \title Qt Android Notifier \example platform/androidnotifier + \meta tag {widgets,android,notification} \brief Demonstrates calling Java code from Qt in an Android application. + \ingroup androidplatform \image androidnotifier.png diff --git a/examples/corelib/platform/androidnotifier/main.cpp b/examples/corelib/platform/androidnotifier/main.cpp index 33e77c7018..07eff5d2b0 100644 --- a/examples/corelib/platform/androidnotifier/main.cpp +++ b/examples/corelib/platform/androidnotifier/main.cpp @@ -41,11 +41,11 @@ int main(int argc, char *argv[]) widget.setLayout(&mainLayout); //! [Connect button signals] - QObject::connect(&happyButton, &QPushButton::clicked, []() { + QObject::connect(&happyButton, &QPushButton::clicked, &happyButton, []() { NotificationClient().setNotification("The user is happy!"); }); - QObject::connect(&sadButton, &QPushButton::clicked, []() { + QObject::connect(&sadButton, &QPushButton::clicked, &happyButton, []() { NotificationClient().setNotification("The user is sad!"); }); //! [Connect button signals] diff --git a/examples/corelib/platform/androidnotifier/notificationclient.cpp b/examples/corelib/platform/androidnotifier/notificationclient.cpp index af1cb7322a..aa6093c29c 100644 --- a/examples/corelib/platform/androidnotifier/notificationclient.cpp +++ b/examples/corelib/platform/androidnotifier/notificationclient.cpp @@ -5,10 +5,22 @@ #include <QtCore/qjniobject.h> #include <QtCore/qcoreapplication.h> +#include <QtCore/private/qandroidextras_p.h> + +using namespace Qt::StringLiterals; NotificationClient::NotificationClient(QObject *parent) : QObject(parent) { + if (QNativeInterface::QAndroidApplication::sdkVersion() >= __ANDROID_API_T__) { + const auto notificationPermission = "android.permission.POST_NOTIFICATIONS"_L1; + auto requestResult = QtAndroidPrivate::requestPermission(notificationPermission); + if (requestResult.result() != QtAndroidPrivate::Authorized) { + qWarning() << "Failed to acquire permission to post notifications " + "(required for Android 13+)"; + } + } + connect(this, &NotificationClient::notificationChanged, this, &NotificationClient::updateAndroidNotification); } |