AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

[android] Multiupload service

Ein Thema von danten · begonnen am 3. Mär 2018
Antwort Antwort
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#1

[android] Multiupload service

  Alt 3. Mär 2018, 18:38
Hello everyone.
How can I create a service that will check the contents of a folder and finds the file by extension so it sends it to the FTP server.

I do not know if you need to use Indy idFTP or else you can do it.

The service should run only when the application is running.

I found a Demo demo for a service downloader, but uploading does not know how to do as a thread.

My Code error: application stoped !!

Thanks


Delphi-Quellcode:
// service
unit fDownloader;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Android.Service,
  AndroidApi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Os,
  System.SyncObjs,
  System.Generics.Collections,
  Network, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase, IdFTP;

type
  TThreadUploader = class;

  TDM = class(TAndroidService)
    function AndroidServiceStartCommand(const Sender: TObject;
      const Intent: JIntent; Flags, StartId: Integer): Integer;
    procedure AndroidServiceCreate(Sender: TObject);
    procedure AndroidServiceDestroy(Sender: TObject);
  private
    { Private declarations }
    FThreads: TObjectList<TThread>;
    FThreadsUploader: TList<TThreadUploader>;

    procedure UploadFoto(AEvent: TEvent);
  public
    { Public declarations }
    procedure MultiUpload;
  end;

  TThreadUploader = class(TThread)
  private
    FEvent: TEvent;

  public
    procedure Execute; override;

    property Event: TEvent read FEvent write FEvent;
  end;

var
  DM: TDM;
  FDir: string;
  fsFilter,fsName,s: string;
  DirImage: string;
  DirSettings: string;
  FTP: TidFTP;
  slSettings: TStringList;

implementation

{%CLASSGROUP 'FMX.Controls.TControl'}

{$R *.dfm}

uses
  Androidapi.JNI.App, Math, System.IOUtils;

procedure TDM.AndroidServiceCreate(Sender: TObject);
begin
  FThreads := TObjectList<TThread>.Create;
  FThreadsUploader := TList<TThreadUploader>.Create;
end;

procedure TDM.AndroidServiceDestroy(Sender: TObject);
begin
  FThreads.Free;
  FThreadsUploader.Free;
end;

function TDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
var
  lThread: TThread;
begin
  lThread := TThread.CreateAnonymousThread(
    procedure
    var
      LEvent: TEvent;
    begin
      LEvent := TEvent.Create();

      UploadFoto(LEvent);

      LEvent.WaitFor(INFINITE);

      JavaService.stopSelf(StartId);
    end
    );

    LThread.FreeOnTerminate := True;
    FThreads.Add(LThread);
    LThread.Start();

  Result := TJService.JavaClass.START_STICKY;
end;

procedure TDM.UploadFoto(AEvent: TEvent);
var
  LThread: TThreadUploader;
begin
  LThread := TThreadUploader.Create(True);
  LThread.FreeOnTerminate := True;
  LThread.Event := AEvent;
  FThreadsUploader.Add(LThread);
  LThread.Start();
end;

procedure TDM.MultiUpload;
var
  isOK: boolean;
begin
  if IsConnected = True then
  begin
  try
    fsFilter := '*.jpeg';

    for fsName in TDirectory.GetFiles(DirImage, fsFilter) do
    begin
      if (fsName <> '') and (FileExists(DirSettings + System.SysUtils.PathDelim + 'settings.ini')) then
      begin


        slSettings := TStringList.Create;

        slSettings.LoadFromFile(DirSettings + System.SysUtils.PathDelim + 'settings.ini');

        FTP := TidFTP.Create(nil);

        FTP.Host := slSettings[8];

        FTP.Port := StrToInt(slSettings[9]);

        FTP.Username := slSettings[10];

        FTP.Password := slSettings[11];

        FDir := slSettings[12];

        FTP.Connect;
        FTP.ChangeDir(FDir);
        try
          FTP.Delete(TPath.GetFileName(fsName));
        except
          isOK := False;
        end;

        FTP.Put(fsName, TPath.GetFileName(fsName));
        DeleteFile(fsName);

        FTP.Site('CHMOD 777 '+ TPath.GetFileName(fsName));
        FTP.Disconnect;

        FTP.Free;


        slSettings.Free;
      end;
    end;
  except
    on E: exception do
    begin
      s := E.Message;
    end;
  end;
  end;
end;

procedure TThreadUploader.Execute;
begin

  while not Terminated do
  begin
    dm.MultiUpload;

    Sleep(10000);

    FEvent.SetEvent;

    Terminate;
  end;
end;

end.
Daniel

Geändert von danten ( 3. Mär 2018 um 22:42 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:07 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz