v0.0.9
This commit is contained in:
parent
f59704545b
commit
69086d8093
1 changed files with 370 additions and 134 deletions
151
service.cpp
151
service.cpp
|
|
@ -11,7 +11,6 @@
|
|||
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) {
|
||||
|
|
@ -29,87 +28,95 @@ static void handleWaterLevelUpdate(JNIEnv *env, jobject /*obj*/, jbyteArray data
|
|||
env->ReleaseByteArrayElements(data, bytes, JNI_ABORT);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
using namespace QtJniTypes;
|
||||
using namespace QNativeInterface;
|
||||
// // 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;
|
||||
|
||||
|
||||
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);
|
||||
// // 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>());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
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>()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue