Communication between C++ and Java without using JNI
-
Hi,
QuickAndroid project has released a new version with a feature for communication between C++ and Java without using JNI.
It provides a component called System Dispatcher for message delivery. The design is quite similar to the AppDispatcher from QuickFlux project. But the delivered message can be passed from C++ to Java and vice versa. Data type conversion is handled automatically.
C++
QASystemDispatcher::dispatch(QString name, QVariantMap message);
quickandroid/qasystemdispatcher.h at 4339203dbb87eed0fda611151481216db004385a · benlau/quickandroid
Java
class SystemDispatcher { public static void dispatch ( String name , Map message ) { ... } }
quickandroid/SystemDispatcher.java at 4339203dbb87eed0fda611151481216db004385a · benlau/quickandroid
The supported data types:
int <-> Integer
bool <-> Boolean
QString <-> java.lang.String
QVariantMap <-> java.util.Map (Non-nested)Example code:
quickandroid/examples/quickandroidexample at master · benlau/quickandroidDeliver message from QML
SystemDispatcher.dispatch("Notifier.notify",{ title: "Quick Android Example", message: "Hello!" });
quickandroid/NotificationDemo.qml at d76397a9c21732439fa9dffeff9bc6246e67e2bc · benlau/quickandroid
Handle the message from Java
public void onDispatched(String name , Map message) { if (name.equals("Notifier.notify")) { notificationManagerNotify(data); } return; }
quickandroid/ExampleActivity.java at d76397a9c21732439fa9dffeff9bc6246e67e2bc · benlau/quickandroid