我想创建一个前台服务,在qt中即使关闭应用程序也能正常工作。我阅读了https://doc.qt.io/qt-5/android-services.html。简单的服务可以成功地运行,但是当我尝试在我的函数中启动前台服务时出现问题。
我做错了什么?为什么 "startService" 能运行,但 "startForeground" 不能运行?几乎没有改变,我只是创建了 "notification" 并使用另一个函数启动服务。
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" 不能运行?几乎没有改变,我只是创建了 "notification" 并使用另一个函数启动服务。