How to use the Qt Jni class "QAndroidJniObject"

4.6k Views Asked by At

I'm a beginner of "Qt for Android" and now I'm using it to develop a communication oftware based on mobile.I hava developed java functions that call android's api as a class in a .java document.In order to Simplify UI development,the UI is based on Qt Widget program.Then I use the Qt-JNI class "QAndroidJniObject" to call these java functions according to Qt5.2 API document.Android resource file is stored in the directory:./android/src/com/comm/sipcall/SipCallSend.java.Since the less information in this regard,I developed the java and c++ program according to API document.But I encountered the following problems, and also hope to get answers: 1) The Qt program is based on Qt Widget.The java program needs to get the current application object Context in order to initialize java object.The c++ code I developed as following:

//c++:
    QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
    jobject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");
    QAndroidJniObject* at = new QAndroidJniObject(activity);
    QAndroidJniObject appctx = at->callObjectMethod("getApplicationContext","()Landroid/content/Context;");

//.pro:
    QT += core gui gui-private

Is this right?

2) The java class contains a constructor function and three public functions:

java:
package com.comm.sipcall;
improt XXXX
....
....
public class SipCallSend extends Activity {
    private Context context; // 接收QT的context
    public String sipToAddress = "";
    public String sipDomain = "";
    public String user_name = "";
    public String pass_word = "";

        public SipCallSend(){

        Log.i("ddd","init");
        sipToAddress = "";
        sipDomain = "";
        user_name = "";
        pass_word = "";
        }
        public void SetContext(Context cnt)
        {
            this.context = cnt;
            //Log.i("ccc",user_name);
        }
        public int Login(String domain,String username,String password){
            ....
            sipDomain = domain;
            user_name = username;
            pass_word = password;
            ...
            return 0;
        }
        public int Call(String addrNum) {
            ...
            return 0;
        }
}

C++:
    QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
    jobject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");
    QAndroidJniObject* at = new QAndroidJniObject(activity);
    QAndroidJniObject appctx = at->callObjectMethod("getApplicationContext","()Landroid/content/Context;");
    QAndroidJniObject* m_sipcall = new QAndroidJniObject("com/comm/sipcall/SipCallSend");
    if (!m_sipcall->isValid())
        return;
    m_sipcall->callMethod<void>("SetContext","(Landroid/content/Context;)V",
                                            appctx.object<jobject>());

    QAndroidJniObject domain = QAndroidJniObject::fromString("10.3.56.54");
    QAndroidJniObject username = QAndroidJniObject::fromString("1006");
    QAndroidJniObject password = QAndroidJniObject::fromString("1234");
    jint res = m_sipcall->callMethod<jint>("Login","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
                                            domain.object<jstring>(), username.object<jstring>(),password.object<jstring>());

    if (res!=0)
        return;

    QAndroidJniObject addrNum = QAndroidJniObject::fromString("1018");
    res = m_sipcall->callMethod<jint>("Call","(Ljava/lang/String;)I",addrNum.object<jstring>());

The "new QAndroidJniObject" returns non-empty,but the the following functions connot execute,why?

3) I have tried to make the Context from c++ as a parameter of constructor function,but I found the code not running,why?

java:
        public SipCallSend(Context cnt){

        this.context = cnt;
        }
C++:
    QAndroidJniObject m_sipcall("com/comm/sipcall/SipCallSend","(Landroid/content/Context;)V",appctx.object<jobject>());

4) According to Qt5.2 API document,QAndroidJniObject provides a method named "callObjectMethod":

QAndroidJniObject myJavaString; ==> "Hello, Java"
QAndroidJniObject mySubstring = myJavaString.callObjectMethod("substring", "(II)Ljava/lang/String;" 7, 10);

But when I use it as following, the IDE prompts me the parameter are incorrect,why?

jint res = m_sipcall->callMethod<jint>("Login","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
                                        domain.object<jstring>(), username.object<jstring>(),password.object<jstring>());

Thanks...

0

There are 0 best solutions below