Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Calling Java class from Qt (like notification example)

    Mobile and Embedded
    2
    3
    895
    Loading More Posts
    • 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.
    • Qojote
      Qojote last edited by Qojote

      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 Reply Quote 0
      • patrikd
        patrikd last edited by

        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 Reply Quote 1
        • Qojote
          Qojote last edited by

          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 Reply Quote 0
          • First post
            Last post