德尔福 10 西雅图更新1 如何从主机应用程序停止服务



我使用 Delphi 10 Seattle update1,我有一个 android 服务,我从主机应用程序开始,但我不知道如何从主机应用程序停止该服务。谁能告诉我,请问?

您正在使用 TLocalServiceConnection.StartService()方法。 Embarcadero没有提供相应的TLocalServiceConnection.StopService()方法,所以你必须直接调用Android的Context.stopService()方法。

以下是从$(BDS)sourcertlandroidSystem.Android.Service.pas TLocalServiceConnection.startService()的源代码:

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;

您可以将TAndroidHelper.Activity.startService()替换为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;

最新更新