first commit
This commit is contained in:
commit
9bfa1e11fe
1 changed files with 1751 additions and 0 deletions
115
service.cpp
Normal file
115
service.cpp
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
#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);
|
||||
}
|
||||
|
||||
|
||||
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<QAndroidApplication>();
|
||||
// Context context = androidApp->context();
|
||||
QJniObject context = QNativeInterface::QAndroidApplication::context();
|
||||
|
||||
QJniEnvironment env;
|
||||
auto serviceClass = env.findClass(Traits<WaterLevelService>::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<void>(
|
||||
"org/qtproject/example/appwaterLevelSensor/WaterLevelServiceStarter",
|
||||
"startService",
|
||||
"(Landroid/content/Context;)V",
|
||||
context.object<jobject>()); // pass the QtActivity instance );
|
||||
|
||||
// QJniObject activity = QNativeInterface::QAndroidApplication::context();
|
||||
// activity.callMethod<void>("startWaterLevelService", "()V");
|
||||
// Intent serviceIntent(activity, serviceClass);
|
||||
// activity.callMethod<WaterLevelService>("startWaterLevelService");
|
||||
|
||||
// activity.callMethod<()
|
||||
// QJniObject::callStaticMethod<void>(
|
||||
// "org/qtproject/example/appwaterLevelSensor/WaterLevelService",
|
||||
// "startQtAndroidService",
|
||||
// "(Landroid/content/Context;)V",
|
||||
// activity.object<jobject>()
|
||||
// );
|
||||
|
||||
// activity.callMethod<void>("startWaterLevelService", "()V"); // Intent serviceIntent(context, serviceClass);
|
||||
// context.callMethod<ComponentName>("startService", serviceIntent);
|
||||
|
||||
// context.callMethod<ComponentName>(
|
||||
// "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<jstring>()
|
||||
// );
|
||||
|
||||
// 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<void>("startQtAndroidService", context);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue