How to check if network is available on Android ( Delphi XE10.3 )

How to check if network is available on Android ( Delphi XE10.3 )

uses
  // Solution 1
  IdBaseComponent,
  IdComponent,
  IdTCPConnection,
  IdTCPClient,
  // Solution 2
  System.Net.HttpClient;


uses
  // Solution 1
  IdBaseComponent,
  IdComponent,
  IdTCPConnection,
  IdTCPClient,
  // Solution 2
  System.Net.HttpClient;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    if IsInternetConnected then
      Label1.Text := 'Connected'
    else
      Label1.Text := 'No Internet';
  except
    on e: exception do
    begin
      Label1.Text := e.Message;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  try
    if IsInternetConnected2 then
      Label1.Text := 'Connected'
    else
      Label1.Text := 'No Internet';
  except
    on e: exception do
    begin
      Label1.Text := e.Message;
    end;
  end;
end;

function TForm1.IsInternetConnected: boolean;
begin
  with TIdTCPClient.Create(Self) do
  begin
    try
      try
        ReadTimeout := 2000;
        ConnectTimeout := 2000;
        Port := 80;
        Host := 'google.com';
        Connect;
        Disconnect;
        result := true;
      except
        result := false;
      end;
    finally
      free;
    end;
  end;
end;

function TForm1.IsInternetConnected2: boolean;
begin
  with THTTPClient.Create do
  begin
    try
      try
        result := Head('http://google.com').StatusCode < 400;
      except
        result := false;
      end;
    finally
      free;
    end;
  end;
end;

Comments