gpt4 book ai didi

java - 使用 JNI 将字符串从 Java 传递到 C++

转载 作者:可可西里 更新时间:2023-11-01 18:38:30 26 4
gpt4 key购买 nike

我有一个 Android 应用程序可以获取未读的 Facebook 通知和收件箱。该应用程序必须在 QT 中完成,但我几乎不了解 QT C++,所以我在 java 中开发了该应用程序,并使用 JNI 从 QT 调用 java 类。这工作正常,但问题是每次有新的 Facebook 通知/消息时我都需要发送一个插槽(在 QT 端)。

所以我的问题是:每分钟,我如何从 Java 通知 QT 我有一条新消息并发送该字符串?

这是我的java代码:

主要类:

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(每分钟验证是否有新的facebook消息,如果有,则假设通知QT并发送消息以便QT能够发送槽)

private static native void publishNotification(String notification);
....
if (newNotification==true)
publishNotification(responseNotification);
...

QT端

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

构建时出错

In function 'void publisNotification(JNIEnv*, jclass,jstring)'

cannot call member function 'FacebookAnddroid::instance()' without object
FacebookAndroid::instance()->handleNewNotification(QString(nativeString));
in facebookAndroid.cpp

每一个帮助都会很有帮助

最佳答案

如果我不这样做,我可能会选择这条路:

  • 定义一个本地 Java 方法。它将用作来自 Java 端的“信号”
  • 为您的 native 方法实现 C++ 实现。操纵要公布的实例(插槽所有者)
  • 注册你的原生方法

在您的 Java 类中:

class Main Activity {
// ...
private static native void publishNotification(String notification);
// Call it from your Java code as it has a proper implementation

//...
if (newNotification) {
publishNotification(notification);
}
//...

在 C++/Qt 方面:

单例实现:

//in facebookandroid.h
class FacebookAndroid {
public:
FacebookAndroid* instance();
void handleNewNotification(QString notification);

protected:
static FacebookAndroid *s_instance;
};

//in facebookandroid.cpp
FacebookAndroid* FacebookAndroid::s_instance = 0;

FacebookAndroid::FacebookAndroid(QObject *parent) : QObject(parent) {
s_instance = this; // remind your first instanciation here
}

FacebookAndroid* FacebookAndroid::instance() {
return s_instance;
}

本地方法实现:

//In facebookandroid.cpp
static void publishNotifcation(JNIEnv *env, jclass /*clazz*/, jstring notification) {
const char* nativeString = env->GetStringUTFChars(notification, 0);
FacebookAndroid::instance()->handleNewNotification(QString(nativeString));
}

因为我们只能通过一个静态方法来做到这一点,我必须在我的类中定义一个单例来访问一个特定的实例。

方法注册:

//In facebookandroid.cpp
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;
}

经过一些研究,我发现了一个特别有用的 complete example .它的目的是实现 InApp 购买,但机制与我在这个答案中描述的完全相同。

关于java - 使用 JNI 将字符串从 Java 传递到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com