【发布时间】:2016-06-08 02:28:13
【问题描述】:
我正在尝试使用本地服务制作应用程序,以
中的示例为例http://docwiki.embarcadero.com/RADStudio/Seattle/en/Creating_Android_Services
但就我而言,我的服务在启动后不会触发通知(我也不能确定该服务正在运行)。有什么方法可以确保我的服务正在运行?自从我尝试检查设备的系统 - 应用程序 - 运行 时,甚至我的应用程序都没有列出。就像我的应用在失去焦点/切换到其他应用后刚刚死机(或休眠)
下面是我的简单服务和通知代码
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;
这是我的调用者应用代码
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;
我尝试在应用程序上放置一个按钮并执行相同的操作(从应用程序发送通知),当我按下按钮时,我的 Text1 组件显示正确的内容,并且通知确实出现了,但在启动我的应用程序时没有出现(应该由服务启动)。服务名称应该是正确的,因为当我更改服务名称时它被强制停止(分段错误 11)
请多多指教。谢谢
【问题讨论】:
标签: android delphi android-service delphi-10-seattle