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. Calling Java class from Qt (like notification example)

Calling Java class from Qt (like notification example)

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 1.1k Views
  • 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.
  • QojoteQ Offline
    QojoteQ Offline
    Qojote
    wrote on last edited by Qojote
    #1

    The notification example of Qt shows how to call java code and it works like charme.
    Now when i take the same code and copy it into my project, the app crashes.
    Thanks to debug prints i know that the java function notify is called but getSystemService hangs.

    package org.tud.qpcam;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;
    
    public class AndroidWifiHandler extends org.qtproject.qt5.android.bindings.QtActivity
    {
        private static NotificationManager m_notificationManager;
        private static Notification.Builder m_builder;
        private static AndroidWifiHandler m_instance;
    
        public AndroidWifiHandler()
        {
            m_instance = this;
        }
    
        public static void notify(String s)
        {
            System.out.println(s);
    
            if (m_notificationManager == null)
            {
                System.out.println(m_instance);
                m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
                System.out.println(m_notificationManager);
                m_builder = new Notification.Builder(m_instance);
                m_builder.setSmallIcon(R.drawable.icon);
                m_builder.setContentTitle("A message from Qt!");
            }
    
            m_builder.setContentText(s);
            m_notificationManager.notify(1, m_builder.build());
        }
    }
    

    and calling function code is:

     QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
     QAndroidJniObject::callStaticMethod<void>("org/tud/qpcam/AndroidWifiHandler",
                                           "notify",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    

    What could be the reason for that? I took the source file an copied it and i changed the package name.

    EDIT:
    The problem is that m_instance is NULL, so getSystemService fails.

    Best regards

    1 Reply Last reply
    0
    • patrikdP Offline
      patrikdP Offline
      patrikd
      wrote on last edited by
      #2

      Hi Qojote,
      not sure why its not working, but try following:
      Imports:

      import org.qtproject.qt5.android.bindings.QtApplication;
      import org.qtproject.qt5.android.bindings.QtActivity;
      import org.qtproject.qt5.android.QtNative;
      
      public class AndroidWifiHandler extends QtActivity 
      

      and don't forget to update your manifest:

      <application android:name="org.qtproject.qt5.android.bindings.QtApplication" ...
      <activity android:name="org.qtproject.qt5.android.bindings.AndroidWifiHandler"....
      

      hope this helps,
      patrik

      1 Reply Last reply
      1
      • QojoteQ Offline
        QojoteQ Offline
        Qojote
        wrote on last edited by
        #3

        Hi patrik,

        Thanks a lot. The manifest entry for activity android:name had to be changed. Now it works like expected.

        1 Reply Last reply
        0

        • Login

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