Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Android service binding using jni



  • I’m having an issue with QtAndroidService. Basically I have an application which has a service that must be usable by other application “on-request”: for this task I thought to use the android service binding mechanism which seems to provide exactly what I want: start the service by third party application if isn’t already started and stop the service only when the service has no binded application left.

    To start a service from an android app I use the following code:

    package com.example.secondapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.Messenger;
    
    public class MainActivity extends AppCompatActivity {
    
        Messenger mService  = null;
        boolean bound;
    
        private ServiceConnection mConnection = new ServiceConnection(){
            public void onServiceConnected(ComponentName className, IBinder service)
            {
                mService = new Messenger(service);
                bound = true;
            }
    
            public void onServiceDisconnected(ComponentName className)
            {
                mService = null;
                bound = false;
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent i = new Intent();
            i.setComponent(new ComponentName("org.androidtest", "org.androidtest.QtAndroidService"));
            bindService(i, mConnection,
                    Context.BIND_AUTO_CREATE);
        }
    }
    

    Since the QtAndroidService must call a native c++ function (which uses Qt) my problem is that when the service is started it hasn’t the proper call registered in the JNI environment using the code like the following:

    JNINativeMethod methods[] {
            {"myFuncJavaWrapper", "(Ljava/lang/String;)V", reinterpret_cast<void *>(myFuncNative)}};
        QAndroidJniObject javaClass("org/mytestproject/ActivityUtils");
    
        QAndroidJniEnvironment env;
        jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
        env->RegisterNatives(objectClass,
                             methods,
                             sizeof(methods) / sizeof(methods[0]));
        env->DeleteLocalRef(objectClass);
    

    where myFuncJavaWrapper is the callable from the java environment who is attached to the native c++ call myFuncNative.

    Since the method registering code is the main code application (which usually is started when the QtActivity is started) but instead binding the service only starts the service and so, the call myFuncJavaWrapper inside the service isn’t already registered, can you suggest me a way to manage this in such a way to solve my issue?


Log in to reply