Reputation: 117
I'm trying to make an application with local service, with an example from
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Creating_Android_Services
But in my case, my service just won't fire a notification after started (nor can I be sure that the service is running). Is there any way to make sure my service is running? Since when I tried to check on device's System - Apps - Running even my app is not listed. It's like my app just died (or sleep) after it lost focus/switch to other app
Below is my simple service & notification code
function TAndroidServiceDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
var TheNotif: TNotification;
begin
TheNotif := Notif.CreateNotification;
try
TheNotif.Title := 'Notif Title';
TheNotif.Name := 'NotifServiceStart';
TheNotif.AlertBody := 'I''m alive!!!!';
TheNotif.FireDate := Now;
Notif.PresentNotification(TheNotif);
finally
TheNotif.Free;
end;
Result := TJService.JavaClass.START_STICKY;
end;
and this is my caller app code
procedure TfmMain.FormCreate(Sender: TObject);
var FServiceConn: TLocalServiceConnection;
begin
FServiceConn := TLocalServiceConnection.Create;
FServiceConn.StartService('unMainServiceLocation');
FServiceConn.BindService('unMainServiceLocation');
end;
procedure TfmMain.NotifReceiveLocalNotification(Sender: TObject;
ANotification: TNotification);
begin
Text1.Text :=
'Title : ' + ANotification.Title + #13#10 +
'Name : ' + ANotification.Name + #13#10 +
'Alert : ' + ANotification.AlertBody ;
end;
I tried to put a button on app and do the same thing (send notification from app) and when I pressed the button, my Text1 component shows the right thing, and the notification did appear, but not when starting my app (should've been started by the service). Service name suppose to be right since when I change the service name it got forced stopped (segmentation fault 11)
Please kindly give advice. Thanks
Upvotes: 1
Views: 1341
Reputation: 47
The scope of your variable var FServiceConn: TLocalServiceConnection;
is limited to FormCreate
, meaning it will not be available outside the FormCreate
procedure. Place the variable up it in the private
section of the form. Don't forget to clean up on closing / destroying the form.
Upvotes: 0
Reputation: 1
After having put this lines in other event (like a button click), it works fine:
FServiceConn.StartService('unMainServiceLocation');
FServiceConn.BindService('unMainServiceLocation');
Upvotes: 0