qtandroidservices_example icon indicating copy to clipboard operation
qtandroidservices_example copied to clipboard

How do you run the service?

Open sassanh opened this issue 8 years ago • 2 comments

The service is not running, it's neither shows up in applications list nor it creates the file I asked it to create in its onCreate. (Another file in the same folder is successfully created with onCreate method of the activity just to make sure permissions are alright.) Should I run some code to run the service? Like startIntent or Idk.

sassanh avatar Sep 14 '16 10:09 sassanh

Currently the service only gets started when you start your application (see MyCustomAppActivity.java). I am myself not an Android expert, but as far as I know Android services can be stopped by the operating system if the system needs resources. So maybe that happens to your service?

You could try to use "adb logcat" to check if the service gets started (when it gets started, a log message is printed out)

Bernhard

bbernhard avatar Sep 14 '16 13:09 bbernhard

Thanks for quick reply. Service wasn't getting started for me (checking logcat, Settings > Manage Apps, and the file I created in the onCreate) and as far as I understand it doesn't start automatically. I had to do this:

jclass serviceClass = NULL;
JNIEXPORT jint JNI_OnLoad(JavaVM *, void *) {
    QAndroidJniEnvironment env;
    serviceClass = (jclass)env->NewGlobalRef(env->FindClass("com/x/y/Service"));
    return JNI_VERSION_1_6;
}

and somewhere for example in int main:

    QAndroidJniObject activity = QtAndroid::androidActivity();
    QAndroidJniObject intent("android/content/Intent", "(Landroid/content/Context;Ljava/lang/Class;)V", activity.object<jobject>(), serviceClass);
    QAndroidJniObject componentName = activity.callObjectMethod("startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", intent.object<jobject>());

sassanh avatar Sep 14 '16 13:09 sassanh