How to check if network is available on Android and iOS ( Delphi XE5 )

how to check internet connection in android programmatically
isnetworkavailable android
  • How to check if network is available on Android and iOS?

Try this:

unit Network;

interface

function IsConnected: Boolean;

function IsWiFiConnected: Boolean;

function IsMobileConnected: Boolean;

implementation

uses
  System.SysUtils,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  FMX.Helpers.Android, Androidapi.Helpers, Misc;

type
  JConnectivityManager = interface;
  JNetworkInfo = interface;

  JNetworkInfoClass = interface(JObjectClass)
  ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}']
  end;

  [JavaSignature('android/net/NetworkInfo')]
  JNetworkInfo = interface(JObject)
  ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}']
    {Methods}
    function isAvailable: Boolean; cdecl;
    function isConnected: Boolean; cdecl;
    function isConnectedOrConnecting: Boolean; cdecl;
  end;
  TJNetworkInfo = class(TJavaGenericImport<JNetworkInfoClass, JNetworkInfo>) end;

  JConnectivityManagerClass = interface(JObjectClass)
  ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}']
    {Property methods}
    function _GetTYPE_WIFI: Integer; cdecl;
    function _GetTYPE_WIMAX: Integer; cdecl;
    function _GetTYPE_MOBILE: Integer; cdecl;
    {Properties}
    property TYPE_WIFI: Integer read _GetTYPE_WIFI;
    property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
    property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
  end;

  [JavaSignature('android/net/ConnectivityManager')]
  JConnectivityManager = interface(JObject)
  ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}']
    {Methods}
    function getActiveNetworkInfo: JNetworkInfo; cdecl;
    function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
  end;
  TJConnectivityManager = class(TJavaGenericImport<JConnectivityManagerClass, JConnectivityManager>) end;

function GetConnectivityManager: JConnectivityManager;
var
  ConnectivityServiceNative: JObject;
begin
  ConnectivityServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.CONNECTIVITY_SERVICE);
  if not Assigned(ConnectivityServiceNative) then
    raise Exception.Create('Could not locate Connectivity Service');
  Result := TJConnectivityManager.Wrap(
    (ConnectivityServiceNative as ILocalObject).GetObjectID);
  if not Assigned(Result) then
    raise Exception.Create('Could not access Connectivity Manager');
end;

function IsConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  ActiveNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
  Result := Assigned(ActiveNetwork) and ActiveNetwork.isConnected;
end;

function IsWiFiConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  WiFiNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI);
  Result := WiFiNetwork.isConnected;
end;

function IsMobileConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  MobileNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);
  Result := MobileNetwork.isConnected;
end;

end.

Checking for an internet connection on mobile , I'm using Delphi XE5 and the network API works fine when running on android, but just crashes on iOS 5.1.1. The application just shuts down  If you are looking for a cross platform way to check for connectivity in your Delphi Firemonkey apps on Android and IOS then this might be the solution for you. It is a simple set of free classes that lets you do just that. For IOS it requires a small static library be included as well.

Thanks a lot to blong for solution. For it began to work at Delphi Berlin, I redid the module code. I hope this is useful.

At the RAD 10.1 can be used entirely Androidapi.JNI.Net.pas module. But for the study may be useful to make the proposed functions in a separate module.

unit Androidapi.JNI.Network;

interface

function IsConnected: Boolean;
function IsWiFiConnected: Boolean;
function IsMobileConnected: Boolean;

implementation

uses
  System.SysUtils,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Net,
  FMX.Helpers.Android,
  Androidapi.Helpers;

// type
// JConnectivityManager = interface;
// JNetworkInfo = interface;
//
// JNetworkInfoClass = interface(JObjectClass)
// ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}']
// end;
//
// [JavaSignature('android/net/NetworkInfo')]
// JNetworkInfo = interface(JObject)
// ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}']
// { Methods }
// function isAvailable: Boolean; cdecl;
// function IsConnected: Boolean; cdecl;
// function isConnectedOrConnecting: Boolean; cdecl;
// end;
// TJNetworkInfo = class(TJavaGenericImport<JNetworkInfoClass, JNetworkInfo>)
// end;
//
// JConnectivityManagerClass = interface(JObjectClass)
// ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}']
// { Property methods }
// function _GetTYPE_WIFI: Integer; cdecl;
// function _GetTYPE_WIMAX: Integer; cdecl;
// function _GetTYPE_MOBILE: Integer; cdecl;
// { Properties }
// property TYPE_WIFI: Integer read _GetTYPE_WIFI;
// property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
// property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
// end;
//
// [JavaSignature('android/net/ConnectivityManager')]
// JConnectivityManager = interface(JObject)
// ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}']
// { Methods }
// function getActiveNetworkInfo: JNetworkInfo; cdecl;
// function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
// end;
// TJConnectivityManager = class(TJavaGenericImport<JConnectivityManagerClass, JConnectivityManager>)
// end;

function GetConnectivityManager: JConnectivityManager;
var
  ConnectivityServiceNative: JObject;
begin
  ConnectivityServiceNative := TAndroidHelper.Context.getSystemService
    (TJContext.JavaClass.CONNECTIVITY_SERVICE);
  if not Assigned(ConnectivityServiceNative) then
    raise Exception.Create('Could not locate Connectivity Service');
  Result := TJConnectivityManager.Wrap((ConnectivityServiceNative as ILocalObject).GetObjectID);
  if not Assigned(Result) then
    raise Exception.Create('Could not access Connectivity Manager');
end;

function IsConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  ActiveNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
  Result := Assigned(ActiveNetwork) and ActiveNetwork.IsConnected;
end;

function IsWiFiConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  WiFiNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI);
  Result := WiFiNetwork.IsConnected;
end;

function IsMobileConnected: Boolean;
var
  ConnectivityManager: JConnectivityManager;
  MobileNetwork: JNetworkInfo;
begin
  ConnectivityManager := GetConnectivityManager;
  MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);
  Result := MobileNetwork.IsConnected;
end;

end.

Internet connectivity state management in Firemonkey, It's a common task for a Firemonkey developer to check Internet connectivity. Android and iOS support; ability to retrieve current Internet The completed solution called “firemonkey-network-state” and available on GitHub “​SystemConfiguration” framework for the iOS target in RAD Studio SDK Manager​  I'd like to keep device on and avoid unloading my application from memory even while there is no user activity. Something like service. I made module like How to check if network is available on Android and iOS ( Delphi XE5 ) but system crushes when I run SetWakeLock:

I don't have the source directory in front of me right now but this should help point you in the right direction.

I believe you will be able to redo the below android solution in delphi: Detect whether there is an Internet connection available on Android

Edit: This line replicates the first line of that function exactly, just not sure what type is returned. Once you have that the rest of that function should be trivial:

SharedActivitiyContext.getSystemService(TJContext.JavaClass.ConnectivityService)

Checking for an Internet connection on Mobile Devices with Delphi , Dave Nottage - Checking for an Internet connection on Mobile Devices with Delphi XE5 Download he Network State Demo project zip file at With the release of Delphi XE5 for iOS and Android developers now With the components available on Windows, OSX, iOS and Android, this code is very useful  How to determine if Location service On/Off in firemonkey application? I am aware about the article: How to check if network is available on Android ( Delphi XE5 ). I am not sure that helps me to

uses System.Net.HttpClient                     

function CheckInternet: boolean;
begin
  Result := false;
  with THTTPClient.Create do
  try
    try
      Result := Head('http://google.com').StatusCode < 400;
    except
    end;
  finally
    Free;
  end;
end; 

Check Network Connectivity State With Delphi XE5 Firemonkey On , If you are looking for a cross platform way to check for connectivity in your Connectivity State With Delphi XE5 Firemonkey On Android And IOS on if there is any connectivity at all and whether that connectivity is via Wifi or  25 Free Cross Platform Samples For Delphi’s 25th Anniversary On Android, IOS, OSX, Windows, Linux Speed Up Your Apps With Bitmap Caching In Delphi XE5 Firemonkey On Android And IOS Massive Set Of Free FireMonkey Components On GitHub For Delphi 10.2 Tokyo On Android And IOS

Solution in fmxexpress site.You typically use IdTCPClient component and check if you can connect to google.com

Must Have Check Network Connectivity State Library For #Delphi , Delphi XE5 XE6 XE7 XE8 10 Seattle Berlin Tokyo Rio Firemonkey, Delphi Delphi XE8 Firemonkey Check Status Internet Connection Android IOS You should check if there is an internet connection on Android, IOS, OSX,  You should check if there is an internet connection on Android, IOS, OSX, and Windows prior to attempting to making a connection. The component is free. Even if you are only using the app tethering functionality of Delphi XE8 Firemonkey you should also use this component.

Checking for an internet connection on mobile devices with Delphi , Try this: unit Network; interface function IsConnected: Boolean; function IsWiFiConnected: Boolean; function IsMobileConnected: Boolean; implementation uses DISCLAIMER: This video is boring to the average YouTube viewer. We examine the TApplicationEvent on Android and iOS in Delphi XE5. We secure the data. And we load data. The messages are simply

How to check if a device is connected to a specific wifi network , Checking for an internet connection on mobile devices with Delphi XE5 forums: how to check if a mobile device (either Android or iOS) has an whether there is either a Wifi or Mobile (Cellular data) connection. Bear in mind that for the Android platform you'll need to include the Access Network State  Erik Salaj from Winsoft has released a JDBC library for Delphi XE5 Firemonkey which allows you to access Microsoft SQL Server from Android. The library is I believe a wrapper of the JTDS project which is an open source java driver for MSSQL.

Must Have Check Network Connectivity - FMX Express, Is there a way (multi-platform) to check if the device is actually We aim to get this working on both Android and iOS. Dave Nottage [TeamB] Find tips and code samples at Delphi Worlds: http://delphi.radsoft.com.au/blog XE 5 How to Check Network State Android Firemonkey and How to Check WiFi ? Delphi XE5 Android/iOS - ScrollForm Anderson Jesus. Delphi XE5. DataSnap Android and iOS Client. - Duration: 16:51. How to configure a Shared Network Printer in Windows 7, 8,

Comments
  • this is very raw method. :| is this the normal way to do it on Android platform?
  • It is indeed but cannot think of anything better
  • I added the android tag, possibly gives an answer to the question
  • can you explain what do you mean with check if network is available?
  • Generally getting the state if the network interfaces is up and ready, and the network ( internet connectivity ) is available.
  • What about on iOS? (OP asked about Android and iOS). I use the solution described here: delphiworlds.com/2013/11/…
  • You make a valid point. I was only addressing the Android question. Thanks for the link to an iOS solution.
  • Do not use Indy components. Better use native THttpClient from System.Net.HttpClient . It's more stable and work with https on Android 6 without problems and do not need any additional libraries.