如何在qt android中启动前台服务?

how to start a foreground service in qt android?

我想创建一个前台服务,即使在 qt 中关闭应用程序后也能正常工作。我读了 https://doc.qt.io/qt-5/android-services.html 简单服务成功运行,但是当我尝试在我的函数中启动前台服务时

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);

    }

编译器给我写了这个

:-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.

我做错了什么?为什么“startService”有效,但“startForeground”无效?实际上什么都没有改变,我只是创建“通知”并使用另一个功能启动服务。

您必须在 AndroidManifest.xml

中定义 FOREGROUND_SERVICE 权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

上面这行应该加在<application>

之前

你可以用简单的andorid前台服务来完成。 请从 here.

中找到示例