Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to start a foreground service in qt android?

I want to create a foreground service that will work even after closing the app in qt. I read it https://doc.qt.io/qt-5/android-services.html Simple services work successfully, but when i try to start a foreground service in my function

public static void startQtAndroidService(Context context) {
            /*context.startService(new Intent(context, smsForwardService.class));
            Log.i("smsForwardService", "Service started");
            */
            Intent notificationIntent = new Intent(context, smsForwardService.class);
            PendingIntent pendingIntent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);

            Notification notification =
                      new Notification.Builder(context, "i don't know what is it, so i just put this text here")
                .setContentTitle("SMSForwardService")
                .setContentText("Service working")
                .setContentIntent(pendingIntent)
                .build();

            // Notification ID cannot be 0.
            startForeground(666, notification);

    }

compiler writes me this

:-1: ERROR: D:\projects\FKey\build-FKey-Android_Qt_5_15_1_Clang_Multi_Abi_05487b-Debug\android-build\src\smsForwardService.java:54: error: non-static method startForeground(int,Notification) cannot be referenced from a static context
            startForeground(666, notification);
            ^
Note: Some input files use or override a deprecated API.

What am i doing wrong? Why does "startService" work, but "startForeground" doesn't work? practically nothing has changed, I just create "notification" and start service with another function.

like image 835
Max Barinov Avatar asked Oct 28 '25 15:10

Max Barinov


1 Answers

You have to define FOREGROUND_SERVICE permission in AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

the above line should be added before <application>

You can do it with simple Foreground service of andorid. please find the sample from here.

like image 86
Nensi Kasundra Avatar answered Oct 30 '25 05:10

Nensi Kasundra