#include "service.h" #include #include #include #include #include #include // Q_DECLARE_JNI_CLASS(Intent, "android/content/Intent") Q_DECLARE_JNI_CLASS(ComponentName, "android/content/ComponentName") Q_DECLARE_JNI_CLASS(WaterLevelService, "org/qtproject/example/appwaterLevelSensor/WaterLevelService") static volatile float level = -1; static void handleWaterLevelUpdate(JNIEnv *env, jobject /*obj*/, jbyteArray data) { jbyte *bytes = env->GetByteArrayElements(data, nullptr); jsize len = env->GetArrayLength(data); // Process your data here qDebug() << "Received data from Java broadcast! Length:" << len; level = *((float *) (bytes)); qDebug() << "Level received as: " << level; env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); } service::service(QObject *parent) : QObject{parent} { if(QSysInfo::productType() == "android") { qDebug() << "Running android service"; using namespace QtJniTypes; using namespace QNativeInterface; JNINativeMethod methods[] = { {"updateWaterLevel", "([B)V", (void *)handleWaterLevelUpdate} }; // auto *androidApp = qGuiApp->nativeInterface(); // Context context = androidApp->context(); QJniObject context = QNativeInterface::QAndroidApplication::context(); QJniEnvironment env; auto serviceClass = env.findClass(Traits::className()); if (!serviceClass) { qDebug() << "FAILED to find WaterLevelService class!"; env->ExceptionDescribe(); // VERY important env->ExceptionClear(); return; } jclass javaClass = env.findClass("org/qtproject/example/appwaterLevelSensor/WaterLevelReceiver"); env->RegisterNatives(javaClass, methods, 1); QJniObject::callStaticMethod( "org/qtproject/example/appwaterLevelSensor/WaterLevelServiceStarter", "startService", "(Landroid/content/Context;)V", context.object()); // pass the QtActivity instance ); // QJniObject activity = QNativeInterface::QAndroidApplication::context(); // activity.callMethod("startWaterLevelService", "()V"); // Intent serviceIntent(activity, serviceClass); // activity.callMethod("startWaterLevelService"); // activity.callMethod<() // QJniObject::callStaticMethod( // "org/qtproject/example/appwaterLevelSensor/WaterLevelService", // "startQtAndroidService", // "(Landroid/content/Context;)V", // activity.object() // ); // activity.callMethod("startWaterLevelService", "()V"); // Intent serviceIntent(context, serviceClass); // context.callMethod("startService", serviceIntent); // context.callMethod( // "startForegroundService", // serviceIntent // ); // QJniObject className = QJniObject::fromString( // "org.qtproject.example.appwaterLevelSensor.WaterLevelService" // ); // QJniObject serviceClassObj = QJniObject::callStaticObjectMethod( // "java/lang/Class", // "forName", // "(Ljava/lang/String;)Ljava/lang/Class;", // className.object() // ); // QJniObject intent( // "android/content/Intent", // "(Landroid/content/Context;Ljava/lang/Class;)V", // context.object(), // serviceClassObj.object() // ); // context.callObjectMethod( // "startForegroundService", // "(Landroid/content/Intent;)Landroid/content/ComponentName;", // intent.object() // ); // WaterLevelService::callStaticMethod("startQtAndroidService", context); } }