简体   繁体   中英

get notification using qt-android

I want get notification from my app using qt-android, I found this examole in qt examples , it's in QML and I want use it in QWidgets,To use code in QWidget I changed it as follow:

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_H

notificationclient.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>());
}

For use in Main class:

notification = new NotificationClient(this);

And for get notification:

void myclass::on_btn_clicked(){
notification->setNotification("hello world");
}

and follow code in .pro file too:

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
QT       += core gui androidextras

when on_btn_clicked() called the program suddenly exits

NOTE: This is the java code and I set package name with my app package

我解决了这个问题,我们应该将此属性添加到AndroidMainifest.xml中的活动标签中

android:name="MY.APP.PACKAGE.NAME.NotificationClient"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM