简体   繁体   English

使用qt-android获取通知

[英]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: 我想使用qt-android从我的应用程序获得通知,我在qt示例中发现了这个问题 ,它在QML中,我想在QWidgets中使用它,要在QWidget中使用代码,我将其更改如下:

notificationclient.h 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 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: 适用于Main类:

notification = new NotificationClient(this);

And for get notification: 并获取通知:

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

and follow code in .pro file too: 并遵循.pro文件中的代码:

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

when on_btn_clicked() called the program suddenly exits 当调用该程序的on_btn_clicked()突然退出时

NOTE: This is the java code and I set package name with my app package 注意: 这是Java代码 ,我使用我的应用程序包设置了包名

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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