{"id":1965,"date":"2015-12-03T13:18:14","date_gmt":"2015-12-03T12:18:14","guid":{"rendered":"https:\/\/www.opengis.ch\/?p=1965"},"modified":"2020-04-29T16:05:41","modified_gmt":"2020-04-29T14:05:41","slug":"passing-android-intents-to-qt","status":"publish","type":"post","link":"https:\/\/www.opengis.ch\/it\/2015\/12\/03\/passing-android-intents-to-qt\/","title":{"rendered":"Passing android Intents to Qt"},"content":{"rendered":"<p>Working on QField I had the necessity of passing values from the QtActivity.java to the Qt cpp world, here how I did\u00a0it using an Intent that is sent to the QtActivity (the one you should not edit that comes with Qt). For much more information see <a href=\"https:\/\/www.kdab.com\/qt-android-episode-7\/\">this post series<\/a>.<br \/>\nhopefully this will be helpful to someone.<\/p>\n<pre class=\"lang:java decode:true\" title=\"the Java part\">private void startQtActivity() {\n\tString dotqgis2_dir = \"Test dotqgis2_dir\";\n\tString share_dir = \"Test share_dir\";\n\t\/\/ forward to startQtActivity and finish QFieldActivity\n\tIntent intent = new Intent();\n\tintent.setClass(QFieldActivity.this, QtActivity.class);\n\tintent.putExtra(\"DOTQGIS2_DIR\", dotqgis2_dir);\n        intent.putExtra(\"SHARE_DIR\", share_dir);\n\tstartActivity(intent);\n\tfinish();\n}<\/pre>\n<pre class=\"lang:default decode:true\" title=\"The more elegant cpp\">#include &lt;QAndroidJniObject&gt;\n#include &lt;QtAndroid&gt;\n#include &lt;QDebug&gt;\n#ifdef Q_OS_ANDROID\nQString getExtra(QAndroidJniObject extras, QString extra){\n    if(extras.isValid()){\n        QAndroidJniObject extra_jni = QAndroidJniObject::fromString(extra);\n        extra_jni = extras.callObjectMethod(\"getString\", \"(Ljava\/lang\/String;)Ljava\/lang\/String;\", extra_jni.object&lt;jstring&gt;());\n        if (extra_jni.isValid()){\n            extra = extra_jni.toString();\n            qDebug() &lt;&lt; extra;\n            return extra;\n        }\n    }\n    return \"\";\n}\nvoid getIntentExtras(QStringList intentExtras)\n{\n    QAndroidJniObject activity = QtAndroid::androidActivity();\n    if (activity.isValid()) {\n        qDebug() &lt;&lt; \"activity\";\n        QAndroidJniObject intent = activity.callObjectMethod(\"getIntent\", \"()Landroid\/content\/Intent;\");\n        if (intent.isValid()) {\n            qDebug() &lt;&lt; \"intent: \" &lt;&lt; intent.toString();\n            QAndroidJniObject extras = intent.callObjectMethod(\"getExtras\", \"()Landroid\/os\/Bundle;\");\n            qDebug() &lt;&lt; \"extras: \" &lt;&lt; extras.toString();\n            QString extra;\n            for (int i = 0; i &lt; intentExtras.size(); ++i){\n                extra = intentExtras.at(i).toLocal8Bit().constData();\n                getExtra(extras, extra);\n            }\n          }\n        }\n}\nQStringList intentExtras;\nintentExtras &lt;&lt; \"DOTQGIS2_DIR\" &lt;&lt; \"SHARE_DIR\";\ngetIntentExtras(intentExtras);\n#endif<\/pre>\n<p>This is the first version of the cpp code that I wrote, just for reference. The above code is much cleaner<\/p>\n<pre class=\"lang:default decode:true\" title=\"the simplistic cpp part (the elegant is better)\">#include &lt;QAndroidJniObject&gt;\n#include &lt;QtAndroid&gt;\n#include &lt;QDebug&gt;\nvoid getIntent()\n{\n#ifdef Q_OS_ANDROID\n    QAndroidJniObject activity = QtAndroid::androidActivity();\n    if (activity.isValid()) {\n        qDebug() &lt;&lt; \"activity\";\n        QAndroidJniObject intent = activity.callObjectMethod(\"getIntent\", \"()Landroid\/content\/Intent;\");\n        if (intent.isValid()) {\n            qDebug() &lt;&lt; \"intent: \" &lt;&lt; intent.toString();\n            QAndroidJniObject extras = intent.callObjectMethod(\"getExtras\", \"()Landroid\/os\/Bundle;\");\n            if(extras.isValid()){\n                qDebug() &lt;&lt; \"extras: \" &lt;&lt; extras.toString();\n                QAndroidJniObject dotqgis2_dir = QAndroidJniObject::fromString(\"DOTQGIS2_DIR\");\n                QAndroidJniObject share_dir = QAndroidJniObject::fromString(\"SHARE_DIR\");\n                dotqgis2_dir = extras.callObjectMethod(\"getString\", \"(Ljava\/lang\/String;)Ljava\/lang\/String;\", dotqgis2_dir.object&lt;jstring&gt;());\n                share_dir = extras.callObjectMethod(\"getString\", \"(Ljava\/lang\/String;)Ljava\/lang\/String;\", share_dir.object&lt;jstring&gt;());\n                if (dotqgis2_dir.isValid()){\n                    qDebug() &lt;&lt; dotqgis2_dir.toString();\n                }\n                if (share_dir.isValid()){\n                    qDebug() &lt;&lt; share_dir.toString();\n                }\n            }\n          }\n        }\n#endif\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Working on QField I had the necessity of passing values from the QtActivity.java to the Qt cpp world, here how I did\u00a0it 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. hopefully [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,31],"tags":[125],"class_list":["post-1965","post","type-post","status-publish","format-standard","hentry","category-android","category-cpp","tag-qgis-org"],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":219,"url":"https:\/\/www.opengis.ch\/it\/2011\/05\/28\/gsoc-2011-weekly-report-0\/","url_meta":{"origin":1965,"position":0},"title":"GSoC 2011 weekly report #0","author":"Marco Bernasocchi","date":"28 Maggio 2011","format":false,"excerpt":"so the first (almost) week Is over. In this week I couldn't do much since yesterday I finally graduated. Any how I managed to squeeze into the week some work (together with stuff I had done the previous week).I finished setting up everything and I got a first Qt test\u2026","rel":"","context":"In &quot;Android QGIS&quot;","block_context":{"text":"Android QGIS","link":"https:\/\/www.opengis.ch\/it\/category\/gis\/qfield\/android-qgis\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9699,"url":"https:\/\/www.opengis.ch\/it\/2012\/10\/25\/necessitas-beta1-breaks-qgis-on-android-dont-update-2\/","url_meta":{"origin":1965,"position":1},"title":"Necessitas Beta1 &#034;breaks&#034; qgis on android &#8211; don&#039;t update","author":"Marco Bernasocchi","date":"25 Ottobre 2012","format":false,"excerpt":"Hi all, it has been a while since my last post, and foremost QGIS on android release. I'm very sorry. I've been working hard on another project (inasafe.org) that toke up all my time since we just launched version 1.0. So now to the real problem, necessitas (the android Qt\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":794,"url":"https:\/\/www.opengis.ch\/it\/2012\/10\/25\/necessitas-beta1-breaks-qgis-on-android-dont-update\/","url_meta":{"origin":1965,"position":2},"title":"Necessitas Beta1 &quot;breaks&quot; qgis on android &#8211; don&#039;t update","author":"Marco Bernasocchi","date":"25 Ottobre 2012","format":false,"excerpt":"Hi all, it has been a while since my last post, and foremost QGIS on android release. I'm very sorry. I've been working hard on another project (inasafe.org) that toke up all my time since we just launched version 1.0. So now to the real problem, necessitas (the android Qt\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.opengis.ch\/it\/category\/programming\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":266,"url":"https:\/\/www.opengis.ch\/it\/2011\/08\/09\/gsoc-2011-weekly-report-10\/","url_meta":{"origin":1965,"position":3},"title":"GSoC 2011 weekly report #10","author":"Marco Bernasocchi","date":"9 Agosto 2011","format":false,"excerpt":"This week I finally managed to get a test Qt application packaged as an apk file. The application uses the native Proj lib to do some projections conversions. Basically it does the same as what qgis will do just with much more dependencies. The key problem was that necessitas wipes\u2026","rel":"","context":"In &quot;Android QGIS&quot;","block_context":{"text":"Android QGIS","link":"https:\/\/www.opengis.ch\/it\/category\/gis\/qfield\/android-qgis\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":315,"url":"https:\/\/www.opengis.ch\/it\/2011\/08\/24\/gsoc-2011-final-report\/","url_meta":{"origin":1965,"position":4},"title":"GSoC 2011 final report","author":"Marco Bernasocchi","date":"24 Agosto 2011","format":false,"excerpt":"So, it is over, after 3 months working on QGIS for android as a Google Summer of code project it is now time to wrap up what I did and didn't do. First of all a QGIS android app exists now and it has many features including: - reading\/writing projects\u2026","rel":"","context":"In &quot;Android QGIS&quot;","block_context":{"text":"Android QGIS","link":"https:\/\/www.opengis.ch\/it\/category\/gis\/qfield\/android-qgis\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":874,"url":"https:\/\/www.opengis.ch\/it\/2013\/05\/21\/getting-closer-to-taming-the-snake\/","url_meta":{"origin":1965,"position":5},"title":"Getting closer to taming the snake","author":"Marco Bernasocchi","date":"21 Maggio 2013","format":false,"excerpt":"very geeky but I have to post this: D\/Qt (27512): src\/python\/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: import sys D\/Qt (27512): src\/python\/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: import os D\/Qt (27512): src\/python\/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: sys.path = [\"\/data\/data\/org.qgis.qgis\/files\/share\/python\",\"\/data\/data\/org.qgis.qgis\/files\/\/python\",\"\/data\/data\/org.qgis.qgis\/files\/\/python\" + \"\/plugins\",\"\/data\/data\/org.qgis.qgis\/files\/share\/python\/plugins\"] + sys.path D\/Qt (27512): src\/python\/qgspythonutilsimpl.cpp: 91: (initPython) newpaths: \"\/data\/data\/org.qgis.qgis\/files\/share\/python\",\"\/data\/data\/org.qgis.qgis\/files\/\/python\",\"\/data\/data\/org.qgis.qgis\/files\/\/python\" + \"\/plugins\",\"\/data\/data\/org.qgis.qgis\/files\/share\/python\/plugins\" D\/Qt\u2026","rel":"","context":"In &quot;Android QGIS&quot;","block_context":{"text":"Android QGIS","link":"https:\/\/www.opengis.ch\/it\/category\/gis\/qfield\/android-qgis\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_shortlink":"https:\/\/wp.me\/pbdBtI-vH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/1965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/comments?post=1965"}],"version-history":[{"count":1,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/1965\/revisions"}],"predecessor-version":[{"id":11148,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/posts\/1965\/revisions\/11148"}],"wp:attachment":[{"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/media?parent=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/categories?post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.opengis.ch\/it\/wp-json\/wp\/v2\/tags?post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}