I have an Android app that receives unread notifications from Facebook and Inbox. The application should be executed in QT, but I almost do not know QT C ++, so I developed the application in java and just call the java class from QT using JNI. This works fine, but the fact is that I need to send a slot (on the QT side) every time a new Facebook notification / message appears.
So my question is: every minute, How to notify QT from Java that I have a new message and send a line?
This is my Java code:
MAIN CLASS:
public class MainActivity extends FragmentActivity { ... static public void startFacebookActivity() { String msgTag = "FACEBOOK_APP"; try { Activity mother = QtNative.activity(); Intent intent = new Intent(mother, MainActivity.class); mother.startActivity(intent); } catch (Exception e) { Log.e(msgTag, e.toString()); e.printStackTrace(); } } }
FRAGMENT CLASS (every minute checks if there is a new message on facebook, if so, it is assumed that he must notify QT and send a message so that QT can send a slot)
private static native void publishNotification(String notification); .... if (newNotification==true) publishNotification(responseNotification); ...
QT side
facebookAndroid.cpp
#include "facebookAndroid.h" #include <QtAndroidExtras> FacebookAndroid* FacebookAndroid::s_instance = 0; FacebookAndroid::FacebookAndroid(QObject *parent) : QObject(parent) { s_instance = this;} void FacebookAndroid::startAndroidFacebook() { QAndroidJniObject::callStaticMethod<void>("org.qtproject.example.MainActivity", "startFacebookActivity", "()V"); } FacebookAndroid* FacebookAndroid::instance() { return s_instance; } static void publishNotification(JNIEnv *env, jclass /*clazz*/, jstring notification) { const char* nativeString = env->GetStringUTFChars(notification, 0); FacebookAndroid::instance()->handleNewNotification(QString(nativeString)); } static JNINativeMethod methods[] = { {"publishNotification", "(Ljava/lang/String;)V", (void *)publishNotification} }; jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { JNIEnv *env; if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_4) != JNI_OK) return JNI_FALSE; jclass clazz = env->FindClass("org/qtproject/example/MainActivity"); if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) < 0) return JNI_FALSE; return JNI_VERSION_1_4; }
main.cpp
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QtQuick> #include "facebookAndroid.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; FacebookAndroid sa(&viewer); viewer.rootContext()->setContextProperty(QString("iniciaFacebook"), &sa); viewer.setMainQmlFile(QStringLiteral("qml/FacebookTry/main.qml")); viewer.showExpanded(); return app.exec(); }
facebookAndroid.h
#ifndef FACEBOOKANDROID_H #define FACEBOOKANDROID_H #include <QObject> #include <jni.h> class FacebookAndroid : public QObject { Q_OBJECT public: FacebookAndroid(QObject *parent = 0); FacebookAndroid* instance(); void handleNewNotification(QString notification); protected: static FacebookAndroid *s_instance; public slots: void startAndroidFacebook(); }; #endif // FACEBOOKANDROID_H
BUILDING ERRORS
In function 'void publisNotification(JNIEnv*, jclass,jstring)' cannot call member function 'FacebookAnddroid::instance()' without object FacebookAndroid::instance()->handleNewNotification(QString(nativeString)); in facebookAndroid.cpp
any help would be very helpful