结果:

1.网络连接:是指现在可不可以上网(你非要问我什么是网,我会K你呀的)。

2.WIFI网络:是指现在可以上网,用的是不是WIFI网络(如果你打开了WIFI那它会显示正在使用WIFI)。

3.移动网络:是指现在可以上网,用的是不是移动网络(如果你打开了移动的数据流量它会显示移动网络)。

PS:

打开权限Project -> Options… -> Users permitions-> Access network state -> True。 

第三方单元:

 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,
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 := 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.

实例代码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation,
Androidapi.JNI.Network, //需要引入
FMX.Platform, //需要引入
FMX.ScrollBox, FMX.Memo; type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
procedure Log(s: string);
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} procedure TForm1.Button1Click(Sender: TObject);
begin
if IsConnected then
Label1.Text := '网络连接正常!'
else
Label1.Text := '没有网络连接!';
end; procedure TForm1.Button2Click(Sender: TObject);
begin
if IsWiFiConnected then
Label1.Text := 'WiFi 连接正常!'
else
Label1.Text := 'WiFi 连接断开!';
end; procedure TForm1.Button3Click(Sender: TObject);
begin
if IsMobileConnected then
Label1.Text := '移动网络连接正常!'
else
Label1.Text := '移动网络连接断开!';
end; procedure TForm1.FormCreate(Sender: TObject);
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
else
Log('Application Event Service is not supported.');
end; //监测函数,此处仅仅调用 log 记录一下事件名称,你可以替换为你自己的函数
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
begin
case AAppEvent of
TApplicationEvent.aeFinishedLaunching: Log('Finished Launching');
TApplicationEvent.aeBecameActive: Log('Became Active');
TApplicationEvent.aeWillBecomeInactive: Log('Will Become Inactive');
TApplicationEvent.aeEnteredBackground: Log('Entered Background');
TApplicationEvent.aeWillBecomeForeground: Log('Will Become Foreground');
TApplicationEvent.aeWillTerminate: Log('Will Terminate');
TApplicationEvent.aeLowMemory: Log('Low Memory');
TApplicationEvent.aeTimeChange: Log('Time Change');
TApplicationEvent.aeOpenURL: Log('Open URL');
end;
Result := True;
end; procedure TForm1.Log(s: string);
begin
Memo1.Lines.Add(TimeToStr(Now) + ': ' + s);
end; end.

最新文章

  1. 30分钟学会XAML
  2. 了解JavaScript 面向对象基础 &amp; 原型与对象
  3. js-读取复选框
  4. 进程间通信--fork函数
  5. 使用Javascript来编写贪食蛇(零基础)
  6. 检测浏览器是否支持cookie方法
  7. 最新版本的DBCP数据源配置
  8. bnuoj 20838 Item-Based Recommendation (模拟)
  9. clock_gettime测代码运行时间
  10. 《JavaScript高级程序设计》笔记:BOM(八)
  11. C#操作session的类实例(转)
  12. Linux----------nfs服务器的搭建及常识
  13. Python爬虫-萌妹子图片
  14. JSON语法、对象、遍历数组的区别和基本操作
  15. linux批量替换文件内容3种方法(perl,sed,shell)
  16. GIS中的引擎:地图引擎
  17. Python+OpenCV图像处理(二)——打印图片属性、设置图片存储路径、电脑摄像头的调取和显示
  18. qt-creator
  19. webpack相关文章
  20. English 好的报纸

热门文章

  1. java核心技术记录
  2. POJ3122Pie(二分)
  3. png24是支持Alpha透明的。。。。。。
  4. 【转】notepad++ 应用学习 -- 列模式,十六进制模式
  5. 这些废弃的 HTML 标签不要用
  6. MyBatis的动态SQL操作--更新
  7. Fast Matrix Operations
  8. WCF的通信
  9. Yii cookie操作
  10. java ServerSocket服务端编程