Qt on Android:使用JNI與第三方j(luò)ar包
QHBoxLayout *getLayout = new QHBoxLayout();
layout->addLayout(getLayout);
m_urlEdit = new QLineEdit(http://blog.csdn.net/foruok);
getLayout->addWidget(m_urlEdit, 1);
QPushButton *getButton = new QPushButton(GET);
getLayout->addWidget(getButton);
connect(getButton, SIGNAL(clicked()), this, SLOT(onGet()));
m_resultView = new QTextEdit();
m_resultView->setReadOnly(true);
layout->addWidget(m_resultView, 1);
m_stateLabel = new QLabel();
layout->addWidget(m_stateLabel);
}
Widget::~Widget()
{
}
bool Widget::event(QEvent *e)
{
if(e->type() == SimpleCustomEvent::eventType())
{
e->accept();
SimpleCustomEvent *sce = (SimpleCustomEvent*)e;
switch(sce->m_arg1)
{
case 1:
m_resultView->setText(sce->m_arg2);
m_stateLabel->setText(Success!);
break;
case 0:
m_resultView->setText(sce->m_arg2);
m_stateLabel->setText(Failed!);
break;
case -1:
m_stateLabel->setText(sce->m_arg2);
break;
}
return true;
}
return QWidget::event(e);
}
void Widget::onGet()
{
#ifdef WIN32
m_resultView->setText(Sorry, Just for Android!);
#elif defined(ANDROID)
QString url = m_urlEdit->text();
QAndroidJniObject javaAction = QAndroidJniObject::fromString(url);
QAndroidJniObject::callStaticMethod
downloadWebPage,
(Ljava/lang/String;)V,
javaAction.object
m_stateLabel->setText(Downloading...);
#endif
}
調(diào)用Java的代碼在onGet()槽中,很簡(jiǎn)單,不解釋了。有疑問(wèn)看Qt幫助手冊(cè)有關(guān)QAndroidJniObject類的說(shuō)明。
OK,到此結(jié)束。
評(píng)論