Java Constructor not getting called while using QAndroidJNIObject
-
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..
-
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> <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/> <meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/> <meta-data android:name="android.app.repository" android:value="default"/> <meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/> <meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/> <!-- Deploy Qt libs as part of package --> <meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/> <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/> <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/> <!-- Run with local libs --> <meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/> <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/> <meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/> <meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/> <meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/> <!-- Messages maps --> <meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/> <meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/> <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/> <!-- Messages maps --> <!-- Splash screen --> <!-- <meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/> --> <!-- Splash screen --> </activity> </application> <uses-sdk android:minSdkVersion="18"/> <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/> <!-- 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. --> <!-- %%INSERT_PERMISSIONS --> <!-- 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. --> <!-- %%INSERT_FEATURES -->
</manifest>
@ -
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.