Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to start/stop Android SYSTEM services? (requires rooted phone)



Permlink Replies: 0
Mike Shkolnik

Posts: 19
Registered: 3/11/13
How to start/stop Android SYSTEM services? (requires rooted phone)
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 2, 2017 8:35 PM
Anyone know how to start/stop Android system services?

I need to be able to start and stop an Android system service - specifically:

com.android.location.fused.FusedLocationService

I found a thread for starting and stopping services tied to my application, but I haven't been able to get it working for system services. Below is the start service routine that comes with FireMonkey from System.Android.Service. I also added classes for stopping and checking the application service as it only comes with a starting class by default.
I tried this but merely replacing the app service name with a system service name doesn't work. 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;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString(LService));
  TAndroidHelper.Activity.startService(LIntent);
end;

[altered slightly to accept any service name and not just com.embarcadero...] The source for this info is here:
https://stackoverflow.com/questions/35527746/delphi-10-seattle-update1-how-can-i-stop-service-from-host-app
I can't comment on that thread as I am not a StackOverflow level 50 user.

Someone posted in suggestions for future releases the code for checking the state of services and THIS WORKS for the Android system service I am trying to start and stop so I know I am using the correct service name (I can start/stop through the Play store app "DisableServices" to test):
class function TLocalServiceConnection.isServiceRunning(servicename:string): 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 = servicename);
  end;
end;

Note that I got help elsewhere with getting superuser access for the app, which appears to work. This is required for starting/stopping a system service. Any idea what I need to do different to start/stop a system service?

Edited by: Mike Shkolnik on Sep 2, 2017 8:35 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02