我是“Qt for Android”的初学者,现在我用它来开发基于mobile的软件通信。我开发了java函数,将android的api称为.java文档中的类。为了简化UI开发,UI基于Qt Widget程序。然后我使用Qt-JNI类“QAndroidJniObject”根据Qt5.2 API文档调用这些java函数.Android资源文件存储在目录:./ android /src/com/comm/sipcall/SipCallSend.java。由于这方面的信息较少,我根据API文档开发了java和c ++程序。但是我遇到了以下问题,也希望得到答案: 1)Qt程序基于Qt Widget。java程序需要获取当前的应用程序对象Context才能初始化java对象。我开发的c ++代码如下:
//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
这是对的吗?
2)java类包含一个构造函数和三个公共函数:
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>());
“new QAndroidJniObject”返回非空,但以下函数无法执行,为什么?
3)我试图将c ++中的Context作为构造函数的参数,但我发现代码没有运行,为什么?
java:
public SipCallSend(Context cnt){
this.context = cnt;
}
C++:
QAndroidJniObject m_sipcall("com/comm/sipcall/SipCallSend","(Landroid/content/Context;)V",appctx.object<jobject>());
4)根据Qt5.2 API文档,QAndroidJniObject提供了一个名为“callObjectMethod”的方法:
QAndroidJniObject myJavaString; ==> "Hello, Java"
QAndroidJniObject mySubstring = myJavaString.callObjectMethod("substring", "(II)Ljava/lang/String;" 7, 10);
但是当我按照以下方式使用它时,IDE会提示我参数不正确,为什么?
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>());
...谢谢