I have used Delphi 12 for developing Android application and I'm trying to Service which needs to runs in the background to receive the frequent messages from Server using TCP sockets to displays the notifications. But, the service is deactivated / closed after the main process application is closed.
Below, the code is mentioned the code to start the service :
procedure TForm1.ButtonServiceStartClick(Sender: TObject);
var
ServiceIntent: JIntent;
begin
ServiceIntent := TJIntent.Create;
ServiceIntent.setClassName(TAndroidHelper.Context, StringToJString('com.embarcadero.services.MyService'));
TAndroidHelper.Context.startService(ServiceIntent);
end;
and also, I have added the following service information in AndroidManifest.template.xml :
<service android:enabled="true" android:exported="false" android:label="MyService"
android:process=":remote" android:name="com.embarcadero.services.MyService">
</service>
Code in Service:
function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
end;
I have tried to use this Solution, but not able to compile.
And tried to implement the Sleep in Android Start command / run the thread, still the android service is getting closed.
Do, I need to mentioned the different flag for starting the service or do I need to add additional code to run the service all the time or can we restart the service once it's destroyed and how to handle the code in Destroy event?