我的Delphi XE7为Androiod设备开发的项目需要标题中声明的函数。
我找到了一段代码可以在Windows和IOS平台上运行并获得正确的结果。但在Android设备上,结果始终为:IP地址= 127.0.0.1,子网掩码为空。
procedure TForm1.RefreshList;
var
LList: TIdStackLocalAddressList;
I: Integer;
AAddresses: TStrings;
begin
AAddresses := TStringList.Create;
try
TIdStack.IncUsage;
try
LList := TIdStackLocalAddressList.Create;
try
// for backwards compatibility, return only IPv4 addresses
GStack.GetLocalAddressList(LList);
if LList.Count > 0 then begin
AAddresses.BeginUpdate;
try
for I := 0 to LList.Count-1 do begin
if LList[I].IPVersion = Id_IPv4 then begin
AAddresses.Add(
LList[I].IPAddress+':'+
TIdStackLocalAddressIPv4(LList[I]).SubNetMask);
end;
end;
finally
AAddresses.EndUpdate;
end;
end;
finally
LList.Free;
end;
finally
TIdStack.DecUsage;
end;
if AAddresses.Count > 0 then
Text1.Text:= AAddresses.Text;
finally
AAddresses.Free;
end;
end;
我发现android.net.wifi.WifiManager.getDhcpInfo
可以获取信息,但我不知道如何在Delphi中使用该接口或者是正确的方法?
答案 0 :(得分:0)
type
JWifiManager = interface;
JDhcpInfo = interface;
JWifiManagerClass = interface( JObjectClass )
['{0238345B-CF08-4139-B943-64900FC845F5}']
function _GetACTION_PICK_WIFI_NETWORK : JString;
function _GetEXTRA_WIFI_INFO : JString;
function _GetWIFI_STATE_CHANGED_ACTION : JString;
property ACTION_PICK_WIFI_NETWORK : JString read _GetACTION_PICK_WIFI_NETWORK;
property EXTRA_WIFI_INFO : JString read _GetEXTRA_WIFI_INFO;
property WIFI_STATE_CHANGED_ACTION : JString read _GetWIFI_STATE_CHANGED_ACTION;
end;
[ JavaSignature( 'android/net/wifi/WifiManager' ) ]
JWifiManager = interface( JObject )
['{28DF429C-6E3E-4AFE-8372-18D8E81734E4}']
function isWifiEnabled : Boolean; cdecl;
function setWifiEnabled( enabled : Boolean ) : Boolean; cdecl;
function getDhcpInfo : JDHCPInfo; cdecl;
function getWifiState : Integer; cdecl;
function getConnectionInfo : JWifiInfo; cdecl;
end;
TJWifiManager = class( TJavaGenericImport< JWifiManagerClass, JWifiManager > )
end;
JDHCPInfoClass = interface( JObjectClass )
[ '{65204FA7-CD50-4EA9-85D6-0A9296A01C40}' ]
end;
[ JavaSignature( 'android/net/DhcpInfo' ) ]
JDHCPInfo = interface( JObject )
[ '{BBA64EF4-C771-4ECE-BD98-D6A706A5137F}' ]
end;
TJDHCPInfo = class( TJavaGenericImport< JDHCPInfoClass, JDHCPInfo > )
end;
procedure TForm1.Button1Click(Sender: TObject);
var
WifiManagerObj: JObject;
WifiManager: JWifiManager;
DhcpInfo: JDHCPInfo;
begin
WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);
try
DhcpInfo:= WifiManager.getDhcpInfo;
text1.Text:= Text1.Text + #13#13 + JStringToString(DhcpInfo.toString);
except on E:System.SysUtils.Exception do
begin
text1.Text:= e.ToString;
end;
end;
end;
上面的代码可以打印出字符串格式的正确信息。 有关定义JDHCPInfoClass和JDHCPInfo接口直接读取字段值的任何建议吗?例如:DhcpInfo.netmask,DhcpInfo.gateway?