结果:
1.网络连接:是指现在可不可以上网(你非要问我什么是网,我会K你呀的)。
2.WIFI网络:是指现在可以上网,用的是不是WIFI网络(如果你打开了WIFI那它会显示正在使用WIFI)。
3.移动网络:是指现在可以上网,用的是不是移动网络(如果你打开了移动的数据流量它会显示移动网络)。
PS:
打开权限Project -> Options… -> Users permitions-> Access network state -> True。
第三方单元:
1 unit Androidapi.JNI.Network; 2 3 interface 4 5 function IsConnected: Boolean; 6 7 function IsWiFiConnected: Boolean; 8 9 function IsMobileConnected: Boolean; 10 11 implementation 12 13 uses 14 System.SysUtils, 15 Androidapi.JNIBridge, 16 Androidapi.JNI.GraphicsContentViewText, 17 Androidapi.JNI.JavaTypes, 18 FMX.Helpers.Android, 19 Androidapi.Helpers;//需要引入 20 21 type 22 JConnectivityManager = interface; 23 JNetworkInfo = interface; 24 25 JNetworkInfoClass = interface(JObjectClass) 26 ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}'] 27 end; 28 29 [JavaSignature('android/net/NetworkInfo')] 30 JNetworkInfo = interface(JObject) 31 ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}'] 32 { Methods} 33 function isAvailable: Boolean; cdecl; 34 function isConnected: Boolean; cdecl; 35 function isConnectedOrConnecting: Boolean; cdecl; 36 end; 37 TJNetworkInfo = class(TJavaGenericImport) end; 38 39 JConnectivityManagerClass = interface(JObjectClass) 40 ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}'] 41 { Property methods} 42 function _GetTYPE_WIFI: Integer; cdecl; 43 function _GetTYPE_WIMAX: Integer; cdecl; 44 function _GetTYPE_MOBILE: Integer; cdecl; 45 { Properties} 46 property TYPE_WIFI: Integer read _GetTYPE_WIFI; 47 property TYPE_WIMAX: Integer read _GetTYPE_WIMAX; 48 property TYPE_MOBILE: Integer read _GetTYPE_MOBILE; 49 end; 50 51 [JavaSignature('android/net/ConnectivityManager')] 52 JConnectivityManager = interface(JObject) 53 ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}'] 54 { Methods} 55 function getActiveNetworkInfo: JNetworkInfo; cdecl; 56 function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl; 57 end; 58 TJConnectivityManager = class(TJavaGenericImport ) end; 59 60 function GetConnectivityManager: JConnectivityManager; 61 var 62 ConnectivityServiceNative: JObject; 63 begin 64 ConnectivityServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.CONNECTIVITY_SERVICE); 65 if not Assigned(ConnectivityServiceNative) then 66 raise Exception.Create('Could not locate Connectivity Service'); 67 Result := TJConnectivityManager.Wrap( 68 (ConnectivityServiceNative as ILocalObject).GetObjectID); 69 if not Assigned(Result) then 70 raise Exception.Create('Could not access Connectivity Manager'); 71 end; 72 73 function IsConnected: Boolean; 74 var 75 ConnectivityManager: JConnectivityManager; 76 ActiveNetwork: JNetworkInfo; 77 begin 78 ConnectivityManager := GetConnectivityManager; 79 ActiveNetwork := ConnectivityManager.getActiveNetworkInfo; 80 Result := Assigned(ActiveNetwork) and ActiveNetwork.isConnected; 81 end; 82 83 function IsWiFiConnected: Boolean; 84 var 85 ConnectivityManager: JConnectivityManager; 86 WiFiNetwork: JNetworkInfo; 87 begin 88 ConnectivityManager := GetConnectivityManager; 89 WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI); 90 Result := WiFiNetwork.isConnected; 91 end; 92 93 function IsMobileConnected: Boolean; 94 var 95 ConnectivityManager: JConnectivityManager; 96 MobileNetwork: JNetworkInfo; 97 begin 98 ConnectivityManager := GetConnectivityManager; 99 MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);100 Result := MobileNetwork.isConnected;101 end;102 103 end.
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, 8 FMX.Controls.Presentation, 9 Androidapi.JNI.Network, //需要引入10 FMX.Platform, //需要引入11 FMX.ScrollBox, FMX.Memo;12 13 type14 TForm1 = class(TForm)15 Button1: TButton;16 Label1: TLabel;17 Button2: TButton;18 Button3: TButton;19 Memo1: TMemo;20 procedure Button1Click(Sender: TObject);21 procedure FormCreate(Sender: TObject);22 procedure Button2Click(Sender: TObject);23 procedure Button3Click(Sender: TObject);24 private25 { Private declarations }26 public27 function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;28 procedure Log(s: string);29 { Public declarations }30 end;31 32 var33 Form1: TForm1;34 35 implementation36 37 { $R *.fmx}38 { $R *.NmXhdpiPh.fmx ANDROID}39 40 procedure TForm1.Button1Click(Sender: TObject);41 begin42 if IsConnected then43 Label1.Text := '网络连接正常!'44 else45 Label1.Text := '没有网络连接!';46 end;47 48 procedure TForm1.Button2Click(Sender: TObject);49 begin50 if IsWiFiConnected then51 Label1.Text := 'WiFi 连接正常!'52 else53 Label1.Text := 'WiFi 连接断开!';54 end;55 56 procedure TForm1.Button3Click(Sender: TObject);57 begin58 if IsMobileConnected then59 Label1.Text := '移动网络连接正常!'60 else61 Label1.Text := '移动网络连接断开!';62 end;63 64 procedure TForm1.FormCreate(Sender: TObject);65 var66 aFMXApplicationEventService: IFMXApplicationEventService;67 begin68 if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then69 aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)70 else71 Log('Application Event Service is not supported.');72 end;73 74 //监测函数,此处仅仅调用 log 记录一下事件名称,你可以替换为你自己的函数75 function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent;76 AContext: TObject): Boolean;77 begin78 case AAppEvent of79 TApplicationEvent.aeFinishedLaunching: Log('Finished Launching');80 TApplicationEvent.aeBecameActive: Log('Became Active');81 TApplicationEvent.aeWillBecomeInactive: Log('Will Become Inactive');82 TApplicationEvent.aeEnteredBackground: Log('Entered Background');83 TApplicationEvent.aeWillBecomeForeground: Log('Will Become Foreground');84 TApplicationEvent.aeWillTerminate: Log('Will Terminate');85 TApplicationEvent.aeLowMemory: Log('Low Memory');86 TApplicationEvent.aeTimeChange: Log('Time Change');87 TApplicationEvent.aeOpenURL: Log('Open URL');88 end;89 Result := True;90 end;91 92 procedure TForm1.Log(s: string);93 begin94 Memo1.Lines.Add(TimeToStr(Now) + ': ' + s);95 end;96 97 end.