#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); } extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env; if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) { return JNI_ERR; } // Register your native method ONCE when the library loads jclass javaClass = env->FindClass("org/qtproject/example/appwaterLevelSensor/WaterLevelReceiver"); if (javaClass) { static JNINativeMethod methods[] = { {"updateWaterLevel", "([B)V", (void *)handleWaterLevelUpdate} }; env->RegisterNatives(javaClass, methods, 1); } return JNI_VERSION_1_6; } service::service(QObject *parent) : QObject{parent} { if(QSysInfo::productType() == "android") { qDebug() << "Running android service"; // QJniEnvironment env; // // if (env->ExceptionCheck()) env->ExceptionClear(); // // static bool nativesRegistered = false; // // if(!nativesRegistered) { // // // jclass javaClass = env.findClass("org/qtproject/example/appwaterLevelSensor/WaterLevelReceiver"); // // // if (javaClass) { // // // JNINativeMethod methods[] = {{"updateWaterLevel", "([B)V", (void *)handleWaterLevelUpdate}}; // // // if (env->RegisterNatives(javaClass, methods, 1) < 0) { // // // qDebug() << "RegisterNatives failed!"; // // // } else { // // // nativesRegistered = true; // // // } // // // } else { // // // qDebug() << "Could not find WaterLevelReceiver class"; // // // } // // // if (env->ExceptionCheck()) env->ExceptionClear(); // // } // using namespace QtJniTypes; // using namespace QNativeInterface; // auto *androidApp = qGuiApp->nativeInterface(); // // Context context = androidApp->context(); // QJniObject context = QNativeInterface::QAndroidApplication::context(); // auto serviceClass = env.findClass(Traits::className()); // if (!serviceClass) { // qDebug() << "FAILED to find WaterLevelService class!"; // env->ExceptionDescribe(); // VERY important // env->ExceptionClear(); // return; // } // if (context.isValid()) { // QJniObject::callStaticMethod< // void>( // "org/qtproject/example/appwaterLevelSensor/WaterLevelServiceStarter", // "startService", // "(Landroid/content/Context;)V", // context.object()); // } } } void service::moveAppToBackground() { QJniObject activity = QNativeInterface::QAndroidApplication::context(); if (activity.isValid()) { qDebug() << "Moved app to background"; activity.callMethod("moveTaskToBack", "(Z)Z", true); } qDebug() << "Closing"; } void service::begin() { // Only call this once the UI is visible QJniObject context = QNativeInterface::QAndroidApplication::context(); QJniObject::callStaticMethod( "org/qtproject/example/appwaterLevelSensor/WaterLevelServiceStarter", "startService", "(Landroid/content/Context;)V", context.object() ); }