【问题标题】:Delphi - How to keep doing queries after closing the android app?Delphi - 关闭android应用程序后如何继续查询?
【发布时间】:2018-02-07 14:46:34
【问题描述】:

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

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

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

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

已经谢谢了

最好的问候...

【问题讨论】:

  • 请在帖子中分享您的代码。
  • 我更新了我的问题。
  • 据我所知,它不应该停止。它是在应用程序关闭后立即停止,还是在一段时间后停止?应用中是否有任何代码可能会停止服务?
  • 查询继续,不关闭主应用程序,返回到主菜单,但是当主应用程序从标签部分完全结束时,查询停止并且不发送通知。在主应用程序中只有 fservice.StartService 命令有,这已经只是使用它来启动服务。没有其他代码。
  • 请将您的逻辑的 TTask 实现更改为匿名线程。它应该开箱即用。

标签: android delphi firemonkey


【解决方案1】:
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.

【讨论】:

  • 感谢您的回答!我按照你说的做了改变,但我没有成功。我更新了我的问题,以便更好地理解问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 2020-07-29
相关资源
最近更新 更多