I made some test application TestPro with Local Android Service TestService2. In TestPro only one button which starts service.
When I click button, in Settings -> Apps -> RUNNING i can see 1 process and 1 service. It’s mean Service running OK.
After that i open another applications, best way is open browser and start surf with it (TestPro became inactive). After some time the system will kill TestPro. If i try to activate the programm it will try to start with no success. System ask Wait or Force close the not responding app (LogCat don’t get “Application.Initialize”). On the second start – the same. On the third time programm starts normally.
Any ideas?
Here is application code:
program TestPro;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in ‘Unit1.pas’ {FrmMain},
Androidapi.Helpers, // TAndroidHelper
AndroidApi.Log, // LOGI
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.ActivityManager in ‘Androidapi.JNI.ActivityManager.pas’;
var
LIntent: JIntent;
{$R *.res}
begin
LogI(‘Application.Initialize’); // !!!
Application.Initialize;
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end.
In Unit1.pas:
unit Unit1;
…
procedure TFrmMain.StartService();
var
LIntent: JIntent;
begin
LIntent := TJIntent.Create;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(),TAndroidHelper.StringToJString(‘com.embarcadero.services.TestService2’));
TAndroidHelper.Activity.startService(LIntent);
end;
procedure TFrmMain.Button1Click(Sender: TObject);
begin
StartService();
end;
Here is Code in Service:
function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
end;
PS. Delphi Berlin, SDK 24.3.3