Android Delphi 10西雅图更新1如何从主机应用停止服务

Android Delphi 10西雅图更新1如何从主机应用停止服务,android,delphi,service,delphi-10-seattle,Android,Delphi,Service,Delphi 10 Seattle,我使用Delphi 10西雅图更新1,我有一个android服务,我从主机应用程序开始,但我不知道如何从主机应用程序停止服务。谁能告诉我吗?您正在使用 TLocalServiceConnection.StartService()方法。Embarcadero没有提供相应的TLocalServiceConnection.StopService()方法,因此您必须直接调用Android的方法 以下是$(BDS)\source\rtl\android\System.android.Service.pas

我使用Delphi 10西雅图更新1,我有一个android服务,我从主机应用程序开始,但我不知道如何从主机应用程序停止服务。谁能告诉我吗?

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

以下是
$(BDS)\source\rtl\android\System.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;
您可以将
tandrodHelper.Activity.startService()替换为
tandrodHelper.Activity.stopService()


您首先是如何启动它的?我使用下一个命令启动它:FLocationServiceConn:=TLocalServiceConnection.cread;FLocationServiceConn.StartService(“LocationService”);正如您所写,我的主机应用程序在调用stopService后崩溃,但服务很好地停止了。柏林编译10.1.更好的方法是使用tandrodhelper.Context与startService或stopService(可能在服务中使用)FYI@PaxBeach:到底是哪种崩溃?@Remy Lebeau我怎么能理解它?
var
  LIntent: JIntent;
begin
  LIntent := TJIntent.Create;
  LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString('com.embarcadero.services.LocationService'));
  TAndroidHelper.Activity.stopService(LIntent);
end;