Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Android Toast in QT
Qt 6.11 is out! See what's new in the release blog

Android Toast in QT

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Bhushan_SureB Offline
    Bhushan_SureB Offline
    Bhushan_Sure
    wrote on last edited by
    #1

    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()
          }
        }
    }
    

    }*

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Bhushan_SureB 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

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

        Bhushan_SureB Offline
        Bhushan_SureB Offline
        Bhushan_Sure
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What was the error ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

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

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            Bhushan_SureB 1 Reply Last reply
            0
            • dheerendraD dheerendra

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

              Bhushan_SureB Offline
              Bhushan_SureB Offline
              Bhushan_Sure
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

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

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • Bhushan_SureB Offline
                  Bhushan_SureB Offline
                  Bhushan_Sure
                  wrote on last edited by Bhushan_Sure
                  #8

                  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.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved