Delphi XE5 取Android网络状态

介绍Delphi XE5 获取Android手机网络状态,通过该代码可以得到Wift是否连接、手机网络是否连接,首先会实现一个类库Androidapi.JNI.Network,然后通过这类库来判断网络状况。

在使用这个类库之前,需要配置手机的权限,否则无法得到信息。

下面是这个判断类库的代码:

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,
FMX.Helpers.Android;

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.

通过这个类库,我们可以调用IsConnected、IsWiFiConnected、IsMobileConnected函数来判断网络状况

function GetNetworkState: string; // 得到当前网络连接状态
var
 NS: TNetworkState;
begin
 NS := TNetworkState.Create;
 try
  if not NS.IsConnected then // 无网络
   result := ‘0’
  else if NS.IsWifiConnected then // wifi
   result := ‘1’
  else if NS.IsMobileConnected then // 移动数据
   result := ‘2’;
 finally
   NS.Free;
 end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值