Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Java Constructor not getting called while using QAndroidJNIObject
Forum Updated to NodeBB v4.3 + New Features

Java Constructor not getting called while using QAndroidJNIObject

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 1 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sam_albuquerque
    wrote on last edited by
    #1

    I am working on modifying the Flickr application demonstrated by Jens Bache-Wiig at "Qt Developer Days in Munich in 2011":https://www.youtube.com/watch?v=k1pnEhyF1pg

    I am trying to share the image(Experimenting with plain text first) using ACTION_SEND Intent. My calls are passing through from QML to C++ and it also passes the Java function. However, it fails because the java code can't find the instance of the QtActivity (From the java file below, I only get the log from m_instance== null. The log for the constructor is never printed.) I have tried to compare my code with the NotificationManager example and I am not able to find anything that would prevent the java constructor from being called.

    What am I missing or doing wrong?

    QT Class file
    @#include "androidfunctions.h"
    #include <QtAndroidExtras>
    #include <QDebug>

    AndroidFunctions::AndroidFunctions(QObject *parent) :
    QObject(parent)
    {
    connect(this, SIGNAL(ShareContentChanged()), this, SLOT(shareContentToAndroid()));
    }

    void AndroidFunctions::setShareContent(const QString &ShareContent)
    {
    if(m_sharecontent == ShareContent){
    return;
    }

    qDebug() << "ShareContent set";
    m_sharecontent = ShareContent;
    m_mimetype="text/plain";
    emit ShareContentChanged();
    

    }

    QString AndroidFunctions::ShareContent() const
    {
    qDebug() << "ShareContent read";
    return m_sharecontent;
    }

    void AndroidFunctions::shareContentToAndroid()
    {
    qDebug() << "Sharing Content to AndroidFunctions";
    QAndroidJniObject javaShareContent = QAndroidJniObject::fromString(m_sharecontent);
    QAndroidJniObject javaMimeType = QAndroidJniObject::fromString(m_mimetype);
    qDebug() << "Declaring AndroidFunctions";
    QAndroidJniObject andfunc("uk/me/samaria/flickshare/AndroidFuctions");
    andfunc.callStaticMethod<void>("uk/me/samaria/flickrshare/AndroidFunctions",
    "shareText",
    "(Ljava/lang/String;Ljava/lang/String;)V",
    javaMimeType.object<jstring>(),
    javaShareContent.object<jstring>()
    );

    }
    @

    Java Class
    @
    package uk.me.samaria.flickrshare;
    //import org.qtproject.qt5.android.bindings.QtActivity;
    import org.qtproject.qt5.android.bindings.QtApplication;

    import android.content.Intent;
    import android.content.Context;
    import android.util.Log;
    import android.os.Bundle;

    public class AndroidFunctions extends org.qtproject.qt5.android.bindings.QtActivity
    {
    private static final String TAG = "FLICKR_SHARE: ";
    private static AndroidFunctions m_instance;
    private Context context;
    private Intent intent;
    public AndroidFunctions()
    {
    Log.i(TAG, "Android Functions Instantiated");
    m_instance = this;
    }

    public  static void shareText(final String mimetype, final String mimetext)
    {
        if(m_instance == null) {
            Log.e(TAG, "text sharing failed");
            return;
        }
        try {
            Log.e(TAG, "text sharing started");
    
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.fr/");
            Log.e(TAG, "text sharing activity starting");
    
            m_instance.startActivity(Intent.createChooser(intent, "Share with"));
            Log.e(TAG, "text sharing activity started");
    
        } catch (Exception e){
            Log.e(TAG, "Exception caught when sharing text!", e);
        }
    }
    

    }
    @

    main.cpp
    @#include <QApplication>
    #include <QtQuick>
    #include <QSysInfo>
    #include "androidfunctions.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    const QString mainQmlApp = QLatin1String("qrc:///main.qml");
    QQuickView view;
    
    AndroidFunctions *androidfunctions = new AndroidFunctions(&view);
    view.engine()->rootContext()->setContextProperty(QLatin1String("androidfunctions"), androidfunctions);
    view.setSource(QUrl(mainQmlApp));
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    
    QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit()));
    

    // view.setGeometry(QRect(100, 100, 360, 640));
    view.show();
    return app.exec();
    }
    @

    contd..

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sam_albuquerque
      wrote on last edited by
      #2

      Finally the manifest:
      @
      <?xml version="1.0"?>
      <manifest package="uk.me.samaria.flickrshare" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
      <application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="@string/app_name">
      <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="@string/app_name" android:screenOrientation="unspecified" android:launchMode="singleTop">
      <intent-filter>
      <action android:name="android.intent.action.EDIT"/>
      <action android:name="android.intent.action.VIEW"/>
      <action android:name="android.intent.action.SEND"/>
      <action android:name="android.intent.action.MAIN"/>
      <category android:name="android.intent.category.LAUNCHER"/>
      <data android:mimeType="text/plain" />

              </intent-filter>
              &lt;meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/&gt;
              &lt;meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/&gt;
              &lt;meta-data android:name="android.app.repository" android:value="default"/&gt;
              &lt;meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/&gt;
              &lt;meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/&gt;
              &lt;!-- Deploy Qt libs as part of package --&gt;
              &lt;meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/&gt;
              &lt;meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/&gt;
              &lt;meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/&gt;
              &lt;!-- Run with local libs --&gt;
              &lt;meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/&gt;
              &lt;meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/&gt;
              &lt;meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/&gt;
              &lt;meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/&gt;
              &lt;meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/&gt;
              &lt;!--  Messages maps --&gt;
              &lt;meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/&gt;
              &lt;meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/&gt;
              &lt;meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/&gt;
              &lt;!--  Messages maps --&gt;
      
              &lt;!-- Splash screen --&gt;
              &lt;!--
              &lt;meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/&gt;
              --&gt;
              &lt;!-- Splash screen --&gt;
          </activity>
      </application>
      <uses-sdk android:minSdkVersion="18"/>
      <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
      
      &lt;!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
           Remove the comment if you do not require these default permissions. --&gt;
      &lt;!-- %%INSERT_PERMISSIONS --&gt;
      
      &lt;!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
           Remove the comment if you do not require these default features. --&gt;
      &lt;!-- %%INSERT_FEATURES --&gt;
      

      </manifest>
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sam_albuquerque
        wrote on last edited by
        #3

        Found the solution to my problem.

        The android:name in the <activity> section of the manifest should be changed to my java Class. uk.me.samaria.flickrshare.AndroidFunctions instead of org.qtproject.qt5.android.bindings.QtActivity

        Now that, I am able to share text, onward to share an Image and see if that works.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved