Android 如何检查Delphi 10中是否正在运行服务

Android 如何检查Delphi 10中是否正在运行服务,android,delphi,delphi-10-seattle,Android,Delphi,Delphi 10 Seattle,大多数投票给Java开发人员的共识似乎是 私有布尔值isMyServiceRunningClass服务类{ ActivityManager=ActivityManager getSystemServiceContext.ACTIVITY\u服务; 对于RunningServiceInfo服务:manager.getRunningServicesInteger.MAX_值{ 如果serviceClass.getName.equalsservice.service.getClassName{ 返回t

大多数投票给Java开发人员的共识似乎是

私有布尔值isMyServiceRunningClass服务类{ ActivityManager=ActivityManager getSystemServiceContext.ACTIVITY\u服务; 对于RunningServiceInfo服务:manager.getRunningServicesInteger.MAX_值{ 如果serviceClass.getName.equalsservice.service.getClassName{ 返回true; } } 返回false; } 但我是一个Delphi开发人员,与10西雅图合作

我在编写函数的过程中走了这么远,然后陷入了困境

什么是r?Androidapi.JNI.App.pas包括JActivityManager和getRunningServices方法,它们返回一个JList,但JList究竟是什么

该.pas文件中没有JRunningServiceInfo的定义。我如何定义它并完成代码

函数TfrmMain.IsServiceStarted:Boolean; 变量 活动服务经理:JObject; 事实管理者:JActivityManager; 名单:JList ;; 迭代器:JIterator; r: s:字符串; 开始 结果:=假; ActivityServiceManager:=TAndroidHelper.Context.getSystemServiceTJContext.JavaClass.ACTIVITY\u服务; FActivityManager:=TJActivityManager.WrapActivityServiceManager作为ILocalObject.GetObjectID; 列表:=FActivityManager.getRunningServicesMAXINT; 迭代器:=List.Iterator; 而Iterator.hasNext和not Result do 开始 r:= s:=JStringToStringr.service.getClassName; 结果:=s='com.embarcadero.services.MyService'; 终止 终止
好吧,我想我已经找到了我一直在寻找的答案,这一切都在于提出正确的问题。在一个中国网站上,我找到了RunningServiceInfo的Delphi包装代码,该代码在提供的Androidapi.JNI.App.pas中缺失

这是ActivityManager的一个完整单元,它早于Delphi 10和Delphi XE5,但省去了我自己编写RunningServiceInfo包装器的麻烦。为什么Embarcadero会忽略这一点

通过生成一个单元Androidapi.JNI.RunningServiceInfo.pas,仅针对JActivityManager_RunningServiceInfo类,我就能够完成我的函数代码。它在测试中编译并生成可预测的结果

uses
  Androidapi.JNI.RunningServiceInfo;

function TfrmHostMain.ServiceIsStarted: Boolean;
var
  ActivityServiceManager: JObject;
  FActivityManager : JActivityManager;
  List: JList;
  Iterator: JIterator;
  ri: JActivityManager_RunningServiceInfo;
  s: String;
begin

  Result := False;
  ActivityServiceManager := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  FActivityManager := TJActivityManager.Wrap((ActivityServiceManager as ILocalObject).GetObjectID);
  List := FActivityManager.getRunningServices(MAXINT);
  Iterator := List.iterator;
  while Iterator.hasNext and (not Result) do
  begin
    ri := TJActivityManager_RunningServiceInfo.Wrap((Iterator.next as ILocalObject).GetObjectID);
    s := JStringToString(ri.service.getClassName);
    Result := (s = 'com.embarcadero.services.MyService');
  end;

end;
这是我制作的Androidapi.JNI.RunningServiceInfo.pas的代码。我希望这个解决方案能帮助其他Delphi开发人员节省时间。我在这上面花了太多时间了

unit Androidapi.JNI.RunningServiceInfo;

interface

uses
  Androidapi.JNI.App
  ,Androidapi.JNIBridge
  ,Androidapi.JNI.GraphicsContentViewText
  ,Androidapi.JNI.JavaTypes
  ,Androidapi.JNI.Os
;

type
{ Class forward declarations }
  JActivityManager_RunningServiceInfo = interface;

{  http://developer.android.com/reference/android/app/ActivityManager.RunningServiceInfo.html Added in API level 1 }
  JActivityManager_RunningServiceInfoClass = interface(JObjectClass)
  ['{8A01CD6D-5177-4F74-A65E-20BAC0B76F0F}']
    { Property Methods }
    function _GetCREATOR: JParcelable_Creator;
    function _GetFLAG_FOREGROUND: Integer;
    function _GetFLAG_PERSISTENT_PROCESS: Integer;
    function _GetFLAG_STARTED: Integer;
    function _GetFLAG_SYSTEM_PROCESS: Integer;
    { Methods }
    function init: JActivityManager_RunningServiceInfo; cdecl;
    { Properties }
    property CREATOR: JParcelable_Creator read _GetCREATOR;
    // Added in API level 5
    property FLAG_FOREGROUND: Integer read _GetFLAG_FOREGROUND;
    // Added in API level 5
    property FLAG_PERSISTENT_PROCESS: Integer read _GetFLAG_PERSISTENT_PROCESS;
    // Added in API level 5
    property FLAG_STARTED: Integer read _GetFLAG_STARTED;
    // Added in API level 5
    property FLAG_SYSTEM_PROCESS: Integer read _GetFLAG_SYSTEM_PROCESS;
  end;

  [JavaSignature('android/app/ActivityManager$RunningServiceInfo')]
  JActivityManager_RunningServiceInfo = interface(JObject)
  ['{CEECA783-977A-4E16-8907-C4F65F25D168}']
    { Property Methods }
    function _GetactiveSince: Int64;
    procedure _SetactiveSince(Value: Int64);
    function _GetclientCount: Integer;
    procedure _SetclientCount(Value: Integer);
    function _GetclientLabel: Integer;
    procedure _SetclientLabel(Value: Integer);
    function _GetclientPackage: JString;
    procedure _SetclientPackage(Value: JString);
    function _GetcrashCount: Integer;
    procedure _SetcrashCount(Value: Integer);
    function _Getflags: Integer;
    procedure _Setflags(Value: Integer);
    function _Getforeground: Boolean;
    procedure _Setforeground(Value: Boolean);
    function _GetlastActivityTime: Int64;
    procedure _SetlastActivityTime(Value: Int64);
    function _Getpid: Integer;
    procedure _Setpid(Value: Integer);
    function _Getprocess: JString;
    procedure _Setprocess(Value: JString);
    function _Getrestarting: Int64;
    procedure _Setrestarting(Value: Int64);
    function _Getservice: JComponentName;
    procedure _Setservice(Value: JComponentName);
    function _Getstarted: Boolean;
    procedure _Setstarted(Value: Boolean);
    function _Getuid: Integer;
    procedure _Setuid(Value: Integer);
    { Methods }
    function describeContents: Integer; cdecl;
    procedure readFromParcel(source: JParcel); cdecl;
    procedure writeToParcel(dest: JParcel; flags: Integer); cdecl;
    { Properties }
    property activeSince: Int64 read _GetactiveSince write _SetactiveSince;
    property clientCount: Integer read _GetclientCount write _SetclientCount;
    // API level 5
    property clientLabel: Integer read _GetclientLabel write _SetclientLabel;
    // API level 5
    property clientPackage: JString read _GetclientPackage write _SetclientPackage;
    property crashCount: Integer read _GetcrashCount write _SetcrashCount;
    // API level 5
    property flags: Integer read _Getflags write _Setflags;
    property foreground: Boolean read _Getforeground write _Setforeground;
    property lastActivityTime: Int64 read _GetlastActivityTime write _SetlastActivityTime;
    property pid: Integer read _Getpid write _Setpid;
    property process: JString read _Getprocess write _Setprocess;
    property restarting: Int64 read _Getrestarting write _Setrestarting;
    property service: JComponentName read _Getservice write _Setservice;
    property started: Boolean read _Getstarted write _Setstarted;
    // API level 5
    property uid: Integer read _Getuid write _Setuid;
  end;
  TJActivityManager_RunningServiceInfo = class(TJavaGenericImport<JActivityManager_RunningServiceInfoClass,
    JActivityManager_RunningServiceInfo>) end;

implementation

procedure RegisterTypes;
begin
  TRegTypes.RegisterType('Androidapi.JNI.RunningServiceInfo.JActivityManager_RunningServiceInfo',
    TypeInfo(Androidapi.JNI.RunningServiceInfo.JActivityManager_RunningServiceInfo));
end;

initialization
  RegisterTypes;
end.

好吧,我想我已经找到了我一直在寻找的答案,这一切都在于提出正确的问题。在一个中国网站上,我找到了RunningServiceInfo的Delphi包装代码,该代码在提供的Androidapi.JNI.App.pas中缺失

这是ActivityManager的一个完整单元,它早于Delphi 10和Delphi XE5,但省去了我自己编写RunningServiceInfo包装器的麻烦。为什么Embarcadero会忽略这一点

通过生成一个单元Androidapi.JNI.RunningServiceInfo.pas,仅针对JActivityManager_RunningServiceInfo类,我就能够完成我的函数代码。它在测试中编译并生成可预测的结果

uses
  Androidapi.JNI.RunningServiceInfo;

function TfrmHostMain.ServiceIsStarted: Boolean;
var
  ActivityServiceManager: JObject;
  FActivityManager : JActivityManager;
  List: JList;
  Iterator: JIterator;
  ri: JActivityManager_RunningServiceInfo;
  s: String;
begin

  Result := False;
  ActivityServiceManager := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  FActivityManager := TJActivityManager.Wrap((ActivityServiceManager as ILocalObject).GetObjectID);
  List := FActivityManager.getRunningServices(MAXINT);
  Iterator := List.iterator;
  while Iterator.hasNext and (not Result) do
  begin
    ri := TJActivityManager_RunningServiceInfo.Wrap((Iterator.next as ILocalObject).GetObjectID);
    s := JStringToString(ri.service.getClassName);
    Result := (s = 'com.embarcadero.services.MyService');
  end;

end;
这是我制作的Androidapi.JNI.RunningServiceInfo.pas的代码。我希望这个解决方案能帮助其他Delphi开发人员节省时间。我在这上面花了太多时间了

unit Androidapi.JNI.RunningServiceInfo;

interface

uses
  Androidapi.JNI.App
  ,Androidapi.JNIBridge
  ,Androidapi.JNI.GraphicsContentViewText
  ,Androidapi.JNI.JavaTypes
  ,Androidapi.JNI.Os
;

type
{ Class forward declarations }
  JActivityManager_RunningServiceInfo = interface;

{  http://developer.android.com/reference/android/app/ActivityManager.RunningServiceInfo.html Added in API level 1 }
  JActivityManager_RunningServiceInfoClass = interface(JObjectClass)
  ['{8A01CD6D-5177-4F74-A65E-20BAC0B76F0F}']
    { Property Methods }
    function _GetCREATOR: JParcelable_Creator;
    function _GetFLAG_FOREGROUND: Integer;
    function _GetFLAG_PERSISTENT_PROCESS: Integer;
    function _GetFLAG_STARTED: Integer;
    function _GetFLAG_SYSTEM_PROCESS: Integer;
    { Methods }
    function init: JActivityManager_RunningServiceInfo; cdecl;
    { Properties }
    property CREATOR: JParcelable_Creator read _GetCREATOR;
    // Added in API level 5
    property FLAG_FOREGROUND: Integer read _GetFLAG_FOREGROUND;
    // Added in API level 5
    property FLAG_PERSISTENT_PROCESS: Integer read _GetFLAG_PERSISTENT_PROCESS;
    // Added in API level 5
    property FLAG_STARTED: Integer read _GetFLAG_STARTED;
    // Added in API level 5
    property FLAG_SYSTEM_PROCESS: Integer read _GetFLAG_SYSTEM_PROCESS;
  end;

  [JavaSignature('android/app/ActivityManager$RunningServiceInfo')]
  JActivityManager_RunningServiceInfo = interface(JObject)
  ['{CEECA783-977A-4E16-8907-C4F65F25D168}']
    { Property Methods }
    function _GetactiveSince: Int64;
    procedure _SetactiveSince(Value: Int64);
    function _GetclientCount: Integer;
    procedure _SetclientCount(Value: Integer);
    function _GetclientLabel: Integer;
    procedure _SetclientLabel(Value: Integer);
    function _GetclientPackage: JString;
    procedure _SetclientPackage(Value: JString);
    function _GetcrashCount: Integer;
    procedure _SetcrashCount(Value: Integer);
    function _Getflags: Integer;
    procedure _Setflags(Value: Integer);
    function _Getforeground: Boolean;
    procedure _Setforeground(Value: Boolean);
    function _GetlastActivityTime: Int64;
    procedure _SetlastActivityTime(Value: Int64);
    function _Getpid: Integer;
    procedure _Setpid(Value: Integer);
    function _Getprocess: JString;
    procedure _Setprocess(Value: JString);
    function _Getrestarting: Int64;
    procedure _Setrestarting(Value: Int64);
    function _Getservice: JComponentName;
    procedure _Setservice(Value: JComponentName);
    function _Getstarted: Boolean;
    procedure _Setstarted(Value: Boolean);
    function _Getuid: Integer;
    procedure _Setuid(Value: Integer);
    { Methods }
    function describeContents: Integer; cdecl;
    procedure readFromParcel(source: JParcel); cdecl;
    procedure writeToParcel(dest: JParcel; flags: Integer); cdecl;
    { Properties }
    property activeSince: Int64 read _GetactiveSince write _SetactiveSince;
    property clientCount: Integer read _GetclientCount write _SetclientCount;
    // API level 5
    property clientLabel: Integer read _GetclientLabel write _SetclientLabel;
    // API level 5
    property clientPackage: JString read _GetclientPackage write _SetclientPackage;
    property crashCount: Integer read _GetcrashCount write _SetcrashCount;
    // API level 5
    property flags: Integer read _Getflags write _Setflags;
    property foreground: Boolean read _Getforeground write _Setforeground;
    property lastActivityTime: Int64 read _GetlastActivityTime write _SetlastActivityTime;
    property pid: Integer read _Getpid write _Setpid;
    property process: JString read _Getprocess write _Setprocess;
    property restarting: Int64 read _Getrestarting write _Setrestarting;
    property service: JComponentName read _Getservice write _Setservice;
    property started: Boolean read _Getstarted write _Setstarted;
    // API level 5
    property uid: Integer read _Getuid write _Setuid;
  end;
  TJActivityManager_RunningServiceInfo = class(TJavaGenericImport<JActivityManager_RunningServiceInfoClass,
    JActivityManager_RunningServiceInfo>) end;

implementation

procedure RegisterTypes;
begin
  TRegTypes.RegisterType('Androidapi.JNI.RunningServiceInfo.JActivityManager_RunningServiceInfo',
    TypeInfo(Androidapi.JNI.RunningServiceInfo.JActivityManager_RunningServiceInfo));
end;

initialization
  RegisterTypes;
end.

对Java开发人员投票最多的共识似乎是。。。否,请参阅getRunningServices的文档:此方法仅用于调试或实现服务管理类型的用户界面。您的评论毫无意义。这两段代码都引用了getRunningServices。你读过Java文档吗?没有,我从来没有读过Java文档。我只是编了些东西,希望它能编译。这太糟糕了,但这不是我的问题。大多数投票给Java开发人员的共识似乎是。。。否,请参阅getRunningServices的文档:此方法仅用于调试或实现服务管理类型的用户界面。您的评论毫无意义。这两段代码都引用了getRunningServices。你读过Java文档吗?没有,我从来没有读过Java文档。我只是编造东西,希望它能编译。这太糟糕了,但这不是我的问题