Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

android - How to check if a service is running in Delphi 10 Seattle

The consensus (most votes) for Java developers seems to be

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

But I am a Delphi developer working with 10 Seattle.

I got this far in coding my function, and got stuck.

What is r? The Androidapi.JNI.App.pas includes the JActivityManager and the getRunningServices method that returns a JList, but a JList of what exactly?

There isn't a definition for a JRunningServiceInfoin that .pas file. How do I define this and complete the code?

function TfrmMain.IsServiceStarted: Boolean;
var
  ActivityServiceManager: JObject;
  FActivityManager : JActivityManager;
  List: JList;
  Iterator: JIterator;
  r: 
  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
    r := 
    s := JStringToString(r.service.getClassName);
    Result := (s = 'com.embarcadero.services.MyService');
  end;

end;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I think I've found the answer I was looking for (it's all in asking the right question). On a Chinese website, I found the Delphi wrapper code for the RunningServiceInfo which is missing from the as-supplied Androidapi.JNI.App.pas

http://blog.csdn.net/flcop/article/details/17190009uses (Chinese)

This is a complete unit for the ActivityManager, that predates Delphi 10 Seattle (Delphi XE5), but saves me the trouble of coding the RunningServiceInfo wrapper myself. Why did Embarcadero leave this out anyway?

By making a unit Androidapi.JNI.RunningServiceInfo.pas, just for the JActivityManager_RunningServiceInfo class, I am able to complete my function code. It compiles and produces predicable results in tests.

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;

And here is the code for the Androidapi.JNI.RunningServiceInfo.pas I made. I hope this solution helps other Delphi developers save time. I spent way too much time on this!

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...