Delphi - 如何在关闭Android应用程序后继续进行查询?

时间:2018-02-07 14:46:34

标签: android delphi firemonkey

我正在从事一个项目。该项目包含一项服务。此服务处理MySQL数据库中的查询,并向用户显示取决于查询结果的通知。但是当我关闭主应用程序时,查询停止。即使主应用程序已关闭,我如何继续进行查询? (就像你知道的聊天应用程序一样)

PS:查询和通知代码已投入使用,并使用PPL(并行编程库)进行查询

PS:我认为代码行没有问题。我想我需要触发主应用程序的服务。

pg.quit(); sys.exit()

https://i.hizliresim.com/nOjO55.gif

非常感谢

最诚挚的问候......

1 个答案:

答案 0 :(得分:0)

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.