Reputation: 11
I need to start a service for Android Oreo in foreground but I can't find an example how to do this in Delphi 10.3 for the app and for the service.
I found hints that I have to use startForegroundService in the app and startForeground in the service but I do not know how and where to use these calls.
In the app I start my service with
FService := TLocalServiceConnection.Create;
FService.StartService('MySvc');
In the service I execute the following statement:
function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
end;
Upvotes: 1
Views: 1678
Reputation: 3602
Example of how to start and stop a service in the foreground:
uses
Androidapi.JNI.Support, Androidapi.Helpers;
procedure TServiceModule.StartForeground;
var
LBuilder: JNotificationCompat_Builder;
begin
LBuilder := TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
LBuilder.setAutoCancel(True);
LBuilder.setContentTitle(StrToJCharSequence('Title'));
LBuilder.setContentText(StrToJCharSequence('Text'));
LBuilder.setSmallIcon(TAndroidHelper.Context.getApplicationInfo.icon);
LBuilder.setTicker(StrToJCharSequence('Caption'));
// 1413 is just a number picked at random
TJService.Wrap(System.JavaContext).startForeground(1413, LBuilder.build);
end;
procedure TServiceModule.StopForeground;
begin
TJService.Wrap(System.JavaContext).stopForeground(True);
end;
Upvotes: 1