Lionking
Lionking

Reputation: 89

Delphi 10 Seattle Update1 how can I stop service from host app

I use Delphi 10 Seattle update1 and I have an android service what I start from host app but I do not know how can I stop the service from the host application. Could anyone tell me, please?

Upvotes: 2

Views: 1764

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598279

You are starting the service using the TLocalServiceConnection.StartService() method. Embarcadero does not provide a corresponding TLocalServiceConnection.StopService() method, so you will have to call Android's Context.stopService() method directly.

Here is the source code for TLocalServiceConnection.startService() from $(BDS)\source\rtl\android\System.Android.Service.pas:

class procedure TLocalServiceConnection.StartService(const AServiceName: string);
var
  LIntent: JIntent;
  LService: string;
begin
  LIntent := TJIntent.Create;
  LService := AServiceName;
  if not LService.StartsWith('com.embarcadero.services.') then
    LService := 'com.embarcadero.services.' + LService;
  LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString(LService));
  TAndroidHelper.Activity.startService(LIntent);
end;

You can replace TAndroidHelper.Activity.startService() with TAndroidHelper.Activity.stopService():

var
  LIntent: JIntent;
begin
  LIntent := TJIntent.Create;
  LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString('com.embarcadero.services.LocationService'));
  TAndroidHelper.Activity.stopService(LIntent);
end;

Upvotes: 3

Related Questions