简体   繁体   English

如何检查网络在 Android 和 iOS 上是否可用(Delphi XE5)

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

  • 如何检查网络在 Android 和 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.

Thanks a lot to blong for solution.非常感谢blong的解决方案。 For it began to work at Delphi Berlin, I redid the module code.因为它开始在 Delphi Berlin 工作,我重新编写了模块代码。 I hope this is useful.我希望这是有用的。

At the RAD 10.1 can be used entirely Androidapi.JNI.Net.pas module.在 RAD 10.1 可以完全使用 Androidapi.JNI.Net.pas 模块。 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.

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我相信你将能够在delphi中重做以下android解决方案: 检测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) SharedActivitiyContext.getSystemService(TJContext.JavaClass.ConnectivityService)

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; 

fmxexpress站点中的解决方案。 通常使用IdTCPClient组件并检查是否可以连接到google.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM