From 633d4b78e47aa06e7f429fa4ea002372a34e51d8 Mon Sep 17 00:00:00 2001 From: Mehran Dehghanian Date: Sat, 8 Jan 2022 10:13:52 +0330 Subject: [PATCH] Add 'Passing android Intents extras to Qt' --- Passing-android-Intents-extras-to-Qt.md | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Passing-android-Intents-extras-to-Qt.md diff --git a/Passing-android-Intents-extras-to-Qt.md b/Passing-android-Intents-extras-to-Qt.md new file mode 100644 index 0000000..cc40b82 --- /dev/null +++ b/Passing-android-Intents-extras-to-Qt.md @@ -0,0 +1,100 @@ +## Passing android Intents to Qt (Pass parameters between intents) ## + +Working on QField I had the necessity of passing values from the QtActivity.java to the Qt cpp world, here how I did it using an Intent that is sent to the QtActivity (the one you should not edit that comes with Qt). +For much more information see [this post series](https://www.kdab.com/qt-android-episode-7/). + +hopefully this will be helpful to someone. + +#### Start QtActivity + +``` +private void startQtActivity() { + String dotqgis2_dir = "Test dotqgis2_dir"; + String share_dir = "Test share_dir"; + // forward to startQtActivity and finish QFieldActivity + Intent intent = new Intent(); + intent.setClass(QFieldActivity.this, QtActivity.class); + intent.putExtra("DOTQGIS2_DIR", dotqgis2_dir); + intent.putExtra("SHARE_DIR", share_dir); + startActivity(intent); + finish(); +} +``` + +``` +#include +#include +#include +#ifdef Q_OS_ANDROID +QString getExtra(QAndroidJniObject extras, QString extra){ + if(extras.isValid()){ + QAndroidJniObject extra_jni = QAndroidJniObject::fromString(extra); + extra_jni = extras.callObjectMethod("getString", "(Ljava/lang/String;)Ljava/lang/String;", extra_jni.object()); + if (extra_jni.isValid()){ + extra = extra_jni.toString(); + qDebug() << extra; + return extra; + } + } + return ""; +} +void getIntentExtras(QStringList intentExtras) +{ + QAndroidJniObject activity = QtAndroid::androidActivity(); + if (activity.isValid()) { + qDebug() << "activity"; + QAndroidJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;"); + if (intent.isValid()) { + qDebug() << "intent: " << intent.toString(); + QAndroidJniObject extras = intent.callObjectMethod("getExtras", "()Landroid/os/Bundle;"); + qDebug() << "extras: " << extras.toString(); + QString extra; + for (int i = 0; i < intentExtras.size(); ++i){ + extra = intentExtras.at(i).toLocal8Bit().constData(); + getExtra(extras, extra); + } + } + } +} +QStringList intentExtras; +intentExtras << "DOTQGIS2_DIR" << "SHARE_DIR"; +getIntentExtras(intentExtras); +#endif +``` + +This is the first version of the cpp code that I wrote, just for reference. The below code is much cleaner. + +``` +#include +#include +#include +void getIntent() +{ +#ifdef Q_OS_ANDROID + QAndroidJniObject activity = QtAndroid::androidActivity(); + if (activity.isValid()) { + qDebug() << "activity"; + QAndroidJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;"); + if (intent.isValid()) { + qDebug() << "intent: " << intent.toString(); + QAndroidJniObject extras = intent.callObjectMethod("getExtras", "()Landroid/os/Bundle;"); + if(extras.isValid()){ + qDebug() << "extras: " << extras.toString(); + QAndroidJniObject dotqgis2_dir = QAndroidJniObject::fromString("DOTQGIS2_DIR"); + QAndroidJniObject share_dir = QAndroidJniObject::fromString("SHARE_DIR"); + dotqgis2_dir = extras.callObjectMethod("getString", "(Ljava/lang/String;)Ljava/lang/String;", dotqgis2_dir.object()); + share_dir = extras.callObjectMethod("getString", "(Ljava/lang/String;)Ljava/lang/String;", share_dir.object()); + if (dotqgis2_dir.isValid()){ + qDebug() << dotqgis2_dir.toString(); + } + if (share_dir.isValid()){ + qDebug() << share_dir.toString(); + } + } + } + } +#endif +} +``` + +[source](https://www.opengis.ch/2015/12/03/passing-android-intents-to-qt/) \ No newline at end of file