Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Hide service notification



  • Hi,

    I implemented a foreground Android service which shows an icon and a notification. It works exactly as I want.

    But I fail to quit the service if I leave my application. I didn't find a way to kill it. I know that first I need to remove the notification. I checked the Android documentation and I know how to do it in pure Java. But it doesn't work if I call it from C++.

    Here are all my failing attempts:

    Java

    public class MuzikaService extends QtService
    {
        public static void hideNotification(Service ctx) {
            Log.d("MuzikaService", "Hide notification static");
            ctx.stopForeground(true);
        }
    }
    

    C++

    QtAndroid::runOnAndroidThread([]{
            QAndroidJniObject::callStaticObjectMethod("cz/jech/muzika/MuzikaService",
                                           "hideNotification",
                                           "(Landroid.app.Service;)V",
                                           QtAndroid::androidService().object()
                                           );
        });
    

    second try:
    Java

    public void hideNotification() {
           Log.d("MuzikaService", "Hide notification");
           stopForeground(true);
       }
    

    C++

    QtAndroid::androidService().callMethod<void>("hideNotification");
    

    and a C++ code only:

    QtAndroid::androidService().callMethod<void>("stopForeground","(Z)V",true);
    

    It all compiles and executes but nothing happens. Even the debug messages from Java are not shown (I use adb logcat to check them because QtCreator doesn't show any logs from a service).

    I'm not sure, when should I call a function on Android UI and when it is not needed. The second question is when it is necessary to define a function as static. I tried to modify some of the examples I found on web but I'm a little lost in the logic, because I never programmed anything in Java.

    What should be the correct way to call the Service. stopForeground(bool) function (documentation)?


Log in to reply