Calling Java class from Qt (like notification example)



  • 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



  • 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



  • Hi patrik,

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.