2

My app done with Delphi 10.3 is starting and monitoring my android service successfully on Android version 7. But above Android Version 8 I get error:

java.lang.IllegalStateException: Not allowed to start Intent ... app is in background uid null

Found that from Version 8 Android has changed policies. I should use startForegroundService but then is me unclear how to respond from service to notify Android and allow to start. (And cannot found Delphi sample example)

Here the start service code

    procedure TfrmStarter.StartBLEService;
    var
        Intent : JIntent;
        NativeComponent : JComponentName;
        PackageName, AppName : JString;

begin
    PackageName := StringToJString('com.embarcadero.LysaAdmin');
    AppName := StringToJString('com.embarcadero.services.BLEService');

    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_MAIN);
    Intent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
    NativeComponent := TJComponentName.JavaClass.init(PackageName, AppName);
    Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK or TJIntent.JavaClass.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    Intent.setComponent(NativeComponent);
    SharedActivity.startService(Intent);   //<< This work's fine Android 7

   // SharedActivity.startForegroundService(Intent); //<< Android 8.1 not getting errors but service is not started
end;

Service StartCommand code

function TDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
 Result := TJService.JavaClass.START_STICKY; 
end;
eustachio
  • 89
  • 2
  • 10
  • 1
    https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten – Freddie Bell Sep 30 '20 at 11:04
  • Seems to be my same problem, but have some difficulties to translate java to delphi code,.. – eustachio Sep 30 '20 at 15:07
  • Which part of this are you struggling with "Use startForegroundService() instead of startService()" – Freddie Bell Sep 30 '20 at 18:14
  • 1
    I have a demo of how to deal with starting a service in the foreground, here: https://github.com/DelphiWorlds/KastriFree/tree/master/Demos/ForegroundService. Note that in the demo it's the *service* that takes care of moving itself into the foreground. Also be aware that the demo may need updating for later versions of Delphi – Dave Nottage Sep 30 '20 at 22:49
  • Great demo runs fine. Tried to call the service from a additional external app nothing happen, have added same procedures as in your MainFrm (ApplicationEventMessageHandler and SendCommand and added in deploy the service file). But if I clone and rename your app can have two apps starting the service. Is there some additional setting to permit a additional app to call the service? – eustachio Oct 01 '20 at 12:48

1 Answers1

2

Delphi provides the ability to start a service in one line

uses System.Android.Service;

...

TLocalServiceConnection.StartService('service_name');

service_name - just service name, without package (com.embarcadero. )!

  • Yes and I am doing this in the main host app. But I need to check and start stop service from second external app, for this reason the start code above. – eustachio Sep 30 '20 at 15:02