Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Android Toast in QT



  • Hi, i am trying for on native android toast but it's not working, please tell me where i am wrong !!

    notificationclient.h

    *#ifndef NOTIFICATIONCLIENT_H
    #define NOTIFICATIONCLIENT_H

    #include <QObject>
    #include <QDebug>
    #include <QString>

    class NotificationClient : public QObject
    {
    Q_OBJECT
    public:
    explicit NotificationClient(QObject *parent = 0);

    signals:

    public slots:
    void updateAndroidNotification();
    };*

    notificationclient.cpp

    *#include "notificationclient.h"
    #include <QtAndroidExtras/QAndroidJniObject>

    NotificationClient::NotificationClient(QObject *parent)
    : QObject(parent)
    {

    }

    void NotificationClient::updateAndroidNotification()
    {
    qDebug()<<"Successfully Connected";

    QString toastMessage = "Your toast message!!!";
    QAndroidJniObject javaNotification = QAndroidJniObject::fromString(toastMessage);
    QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
                                           "makeText",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    

    }*

    notificationclient.java

    *package org.qtproject.example.notification;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.widget.Toast;

    public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
    {
    public static NotificationClient m_instance;

    public NotificationClient()
    {
     m_instance=this;
    }
    
    public static void makeText(final String message)
    {
        m_instance.runOnUiThread(new Runnable() {
                            public void run() {
                               Toast.makeText(m_instance.getApplicationContext(),
                                              message,
                                              Toast.LENGTH_LONG).show();
                           }
                        });
    }
    

    }*

    main.qml

    *import QtQuick 2.9
    import QtQuick.Window 2.2

    Window
    {
    visible: true
    width: Screen.width
    height: Screen.height

    Rectangle
    {
        width: parent.width
        height: parent.height
        color: "red"
    
        MouseArea
        {
          anchors.fill: parent
          onClicked:
          {
              tiiii.updateAndroidNotification()
          }
        }
    }
    

    }*


  • Lifetime Qt Champion

    Hi,

    What version of Qt ?
    What version of Android ?
    Got any error message ?
    Got any warning ?



  • @SGaist Thank you for reply, i solved the error , if anybody needs code ping me :)


  • Lifetime Qt Champion

    What was the error ?


  • Qt Champions 2017

    @Bhushan_Sure you better post the details on how did you resolve the issue. Posting the code should help with example.



  • @dheerendra @SGaist Hello, The code is absouletly fine, the error was in the "package specifying " in manifest.xml and in ".java" file, it was not matching, we have to make sure that package name should be same everywhere.


  • Qt Champions 2017

    Just show those entries here with example in manifest and package & what should be matching will help. It helps the community.



  • Okay Sir @dheerendra

    Manifest.xml

    <manifest package="com.amin.QtAndroidToastJava">
    

    Qtandroidtoast.java

    package com.amin.QtAndroidToastJava;
    

    Notificationclient.cpp

    QAndroidJniObject::callStaticMethod<void>("com/amin/QtAndroidToastJava/QtAndroidToastJava",
                                           "makeText",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    

    these files should have same name while including in Qt project
    (1) In manifest.xml ,package name.
    (2) In java file we have to include Package name .
    (3) Also while calling java function from C++ we have to specify same package details, as specify in parameters of function.

    Above things should be same or it will lead error.


Log in to reply