I'm working on a project. The project contains a service. This service processes queries at a MySQL database and shows the user a notification that depends on the result of the query. But the query stops when I close the main app. How can I keep doing the queries even if the main app is closed? (Like chat apps you know)
PS: Queries and notifications codes are placed in service and queries are made with the PPL (Parallel Programming Library)
PS: I don't think there is a problem with the code lines. I guess I need a triggering with the services for main app.
unit ComPhoneService1;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os, System.Notification, Data.DB, DBAccess, Uni, MemDS,
UniProvider, MySQLUniProvider, System.IOUtils, System.Threading;
type
TDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
MySQLUniProvider1: TMySQLUniProvider;
UniBaglanti: TUniConnection;
UniQuery1: TUniQuery;
function AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
Procedure Sorgu;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DM: TDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
AndroidAPI.JNI.APP;
Procedure TDM.Sorgu;
var
ATask: ITask;
IniFile: TStringList;
Bildirim: TNotification;
begin
ATask := TTask.Create(Procedure()
var
A: Boolean;
begin
A := False;
Repeat
begin
Sleep(10000);
if FileExists(TPath.GetPublicPath + '/pcid.ini', True) = True then
begin
try
IniFile := TStringList.Create;
IniFile.LoadFromFile(TPath.GetPublicPath + '/pcid.ini');
UniQuery1.SQL.Text := 'select * from tblHareketler where PCID=:aydi and Goruldu=:Durum';
UniQuery1.ParamByName('aydi').Value := Trim(IniFile.Text);
UniQuery1.ParamByName('Durum').Value := 'Gorulmedi';
UniQuery1.ExecSQL;
UniQuery1.Open;
if UniQuery1.RecordCount > 0 then
begin
Bildirim := NotificationCenter1.CreateNotification;
Bildirim.AlertBody := 'Yeni eylemler mevcut! Görmek için lütfen tıklayınız';
NotificationCenter1.PresentNotification(Bildirim);
end;
finally
IniFile.Free;
end;
end;
end;
Until A = True;
end);
ATask.Start;
end;
function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Sorgu;
Result := TJService.JavaClass.START_STICKY;
end;
end.
https://i.hizliresim.com/nOjO55.gif
Thanks already
Best Regards...
unit ComPhoneService1;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os, System.Notification, Data.DB, DBAccess, Uni, MemDS,
UniProvider, MySQLUniProvider, System.IOUtils, System.Threading;
type
TDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
MySQLUniProvider1: TMySQLUniProvider;
UniBaglanti: TUniConnection;
UniQuery1: TUniQuery;
function AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
Procedure Sorgu;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DM: TDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
AndroidAPI.JNI.APP;
Procedure TDM.Sorgu;
var
IniFile: TStringList;
Bildirim: TNotification;
begin
TThread.CreateAnonymousThread(procedure()
var
A: Boolean;
begin
A := False;
Repeat
begin
Sleep(10000);
if FileExists(TPath.GetPublicPath + '/pcid.ini', True) = True then
begin
try
IniFile := TStringList.Create;
IniFile.LoadFromFile(TPath.GetPublicPath + '/pcid.ini');
UniQuery1.SQL.Text := 'select * from tblHareketler where PCID=:aydi and Goruldu=:Durum';
UniQuery1.ParamByName('aydi').Value := Trim(IniFile.Text);
UniQuery1.ParamByName('Durum').Value := 'Gorulmedi';
UniQuery1.ExecSQL;
UniQuery1.Open;
if UniQuery1.RecordCount > 0 then
begin
Bildirim := NotificationCenter1.CreateNotification;
Bildirim.AlertBody := 'Yeni eylemler mevcut! Görmek için lütfen tıklayınız';
NotificationCenter1.PresentNotification(Bildirim);
end;
finally
IniFile.Free;
end;
end;
end;
Until A = True;
end).Start;
end;
function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Sorgu;
Result := TJService.JavaClass.START_STICKY;
end;
end.