我希望从我的应用程序中获得使用QWidget的通知,我在qt示例中找到了这位考官,它在QML中,我希望在QWidgets中使用它,在QWidget中使用代码,我对它做了如下修改:
notificationclient.h
#ifndef NOTIFICATIONCLIENT_H
#define NOTIFICATIONCLIENT_H
#include <QObject>
class NotificationClient : public QObject
{
Q_OBJECT
public:
explicit NotificationClient(QObject *parent = 0);
void setNotification(QString notification);
QString notification() const;
signals:
void notificationChanged();
private slots:
void updateAndroidNotification();
private:
QString m_notification;
};
#endif // NOTIFICATIONCLIENT_Hnotificationclient.cpp
#include "notificationclient.h"
#include <QtAndroidExtras/QAndroidJniObject>
NotificationClient::NotificationClient(QObject *parent)
: QObject(parent)
{
connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
m_notification = "";
}
void NotificationClient::setNotification(QString notification)
{
if (m_notification == notification)
return;
m_notification = notification;
emit notificationChanged();
}
QString NotificationClient::notification() const
{
return m_notification;
}
void NotificationClient::updateAndroidNotification()
{
QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
"notify",
"(Ljava/lang/String;)V",
javaNotification.object<jstring>());
}供主班使用:
notification = new NotificationClient(this);为了得到通知:
void myclass::on_btn_clicked(){
notification->setNotification("hello world");
}并遵循.pro文件中的代码:
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
QT += core gui androidextras当on_btn_clicked()调用程序突然退出时
注意:这是java代码和我用我的应用程序包设置了包名
发布于 2014-08-19 21:08:19
我解决了这个问题,我们应该在AndroidMainifest.xml中将这个属性添加到活动标记中。
android:name="MY.APP.PACKAGE.NAME.NotificationClient"https://stackoverflow.com/questions/25391201
复制相似问题