博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi XE5 android 获取网络状态
阅读量:4957 次
发布时间:2019-06-12

本文共 3211 字,大约阅读时间需要 10 分钟。

unit Androidapi.JNI.Network;interfacefunction IsConnected: Boolean;function IsWiFiConnected: Boolean;function IsMobileConnected: Boolean;implementationuses  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) 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) 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.

调用方法:

if  IsConnected  then ShowMessage('IsConnected');if IsWiFiConnected then ShowMessage('IsWiFiConnected');if IsMobileConnected then ShowMessage('IsMobileConnected');

 

转载于:https://www.cnblogs.com/m0488/p/3716879.html

你可能感兴趣的文章
【原】iOS中KVC和KVO的区别
查看>>
OMAPL138学习----DSPLINK DEMO解析之SCALE
查看>>
IoC的基本概念
查看>>
restframework CBV试图的4种方式
查看>>
大图居中,以1920px为例
查看>>
Python3 图片转字符画
查看>>
[C陷阱和缺陷] 第7章 可移植性缺陷
查看>>
人需要治愈
查看>>
linux中configure文件默认执行结果所在位置
查看>>
Spring MVC例子
查看>>
jmeter 断言
查看>>
玩玩小爬虫——抓取时的几个小细节
查看>>
error C4996: 'fopen'
查看>>
Windows向Linux上传文件夹
查看>>
20180104-高级特性-Slice
查看>>
6个SQL Server 2005性能优化工具介绍
查看>>
nginx启动、关闭命令、重启nginx报错open() "/var/run/nginx/nginx.pid" failed
查看>>
day14 Python 内置函数、匿名函数和递归函数
查看>>
BZOJ 3097 Hash Killer I
查看>>
UINavigationController的视图层理关系
查看>>