WaterLevelSensor/service.cpp

122 lines
4.2 KiB
C++
Raw Normal View History

2026-04-01 18:33:19 +05:30
#include "service.h"
#include <QGuiApplication>
#include <QtCore/private/qandroidtypes_p.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qcoreapplication.h>
#include <QSysInfo>
#include <QtCore/private/qandroidextras_p.h>
// 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);
}
2026-04-16 17:18:02 +05:30
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&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;
}
2026-04-01 18:33:19 +05:30
service::service(QObject *parent)
: QObject{parent}
{
if(QSysInfo::productType() == "android") {
qDebug() << "Running android service";
2026-04-16 17:18:02 +05:30
// QJniEnvironment env;
// // if (env->ExceptionCheck()) env->ExceptionClear();
// // static bool nativesRegistered = false;
2026-04-01 18:33:19 +05:30
2026-04-16 17:18:02 +05:30
// // 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;
2026-04-01 18:33:19 +05:30
// auto *androidApp = qGuiApp->nativeInterface<QAndroidApplication>();
2026-04-16 17:18:02 +05:30
// // Context context = androidApp->context();
// QJniObject context = QNativeInterface::QAndroidApplication::context();
// auto serviceClass = env.findClass(Traits<WaterLevelService>::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<jobject>());
// }
2026-04-01 18:33:19 +05:30
}
}
2026-04-16 17:18:02 +05:30
void service::moveAppToBackground()
{
QJniObject activity = QNativeInterface::QAndroidApplication::context();
if (activity.isValid()) {
qDebug() << "Moved app to background";
activity.callMethod<jboolean>("moveTaskToBack", "(Z)Z", true);
}
qDebug() << "Closing";
}
void service::begin() {
// Only call this once the UI is visible
QJniObject context = QNativeInterface::QAndroidApplication::context();
QJniObject::callStaticMethod<void>(
"org/qtproject/example/appwaterLevelSensor/WaterLevelServiceStarter",
"startService",
"(Landroid/content/Context;)V",
context.object<jobject>()
);
}