一个开发者分享他某一个程序的设计思路:在Qt下通过jni得到java Env,从而使用GPS等android API,并且已经实现:
也许因为我对Qt信号和槽的情有独钟,看到emit就感到很亲切,并且被深深的吸引了。那么从Qt for android 的qt工程源码看,到底是如何在android上成功启动并运行的呢?
private void startApp(final boolean firstStart)
{
try
{
ActivityInfo ai=getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
if (!ai.metaData.containsKey("android.app.qt_libs_resource_id"))
{
// No required qt libs ?
// Probably this application was compiled using static qt libs
// or all qt libs are prebundled into the package
m_ministroCallback.libs(null, null, null, 0, null);
return;
}
int resourceId = ai.metaData.getInt("android.app.qt_libs_resource_id");
m_qtLibs=getResources().getStringArray(resourceId);
m_ministroCallback.libs(libs,"QT_IMPORT_PATH=/data/local/qt/imports\tQT_PLUGIN_PATH=/data/local/qt/plugins",
"-platform\tandroid", 0,null);
return;
}
try {
if(!bindService(new Intent(eu.licentia.necessitas.ministro.IMinistro.class.getCanonicalName()),
m_ministroConnection, Context.BIND_AUTO_CREATE))
throw new SecurityException("");
} catch (SecurityException e) { }
}
private IMinistroCallback m_ministroCallback = new IMinistroCallback.Stub(){
@Override
public void libs(finalString[] libs, final String evnVars, final String params,
int errorCode, StringerrorMessage) throws RemoteException {
runOnUiThread(new Runnable() {
@Override
public void run() {
startApplication(libs,evnVars, params);
}
});
}
};
public static void startApplication(String params, String environment)
{
if (params == null)
params ="-platform\tandroid";
synchronized (m_mainActivityMutex)
{
startQtAndroidPlugin();
setDisplayMetrics(m_displayMetricsScreenWidthPixels,
m_displayMetricsScreenHeightPixels,
m_displayMetricsDesktopWidthPixels,
m_displayMetricsDesktopHeightPixels,
m_displayMetricsXDpi,
m_displayMetricsYDpi);
if (params.length()>0)
params="\t"+params;
startQtApp("QtApp"+params,environment);
m_started=true;
}
}
extern "C" int main(int, char **); //use the standard mainmethod to start the application
staticvoid * startMainMethod(void * /*data*/)
{
char ** params;
params=(char**)malloc(sizeof(char*)*m_applicationParams.length());
for (inti=0;i<m_applicationParams.size();i++)
params[i]=(char*)m_applicationParams[i].constData();
int ret = main(m_applicationParams.length(),params);
......
}
static void keyDown(JNIEnv */*env*/, jobject /*thiz*/,jint key, jint unicode, jint modifier)
{
……
int mappedKey=mapAndroidKey(key);
if (mappedKey==Qt::Key_Close)
{
qDebug()<<"handleCloseEvent"<<mLastTLW;
QWindowSystemInterface::handleCloseEvent(mLastTLW);
}
else
QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, mappedKey,modifiers, QChar(unicode),true);
//通过JNI的帮助,转化成了Qt的实现
}