Android手机获取IP地址的两种方法
程序员文章站
2022-07-03 18:54:55
1.使用wifi
首先设置用户权限
复制代码 代码如下:
<uses-permission android:name="android.permission.access_wifi_state"></uses-permission>
<uses-permission android:name="android.permission.change_wifi_state"></uses-permission>
<uses-permission android:name="android.permission.wake_lock"></uses-permission>
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
//获取wifi服务
wifimanager wifimanager = (wifimanager) getsystemservice(context.wifi_service);
//判断wifi是否开启
if (!wifimanager.iswifienabled()) {
wifimanager.setwifienabled(true);
}
wifiinfo wifiinfo = wifimanager.getconnectioninfo();
int ipaddress = wifiinfo.getipaddress();
string ip = inttoip(ipaddress);
edittext et = (edittext)findviewbyid(r.id.edittext01);
et.settext(ip);
}
private string inttoip(int i) {
return (i & 0xff ) + "." +
((i >> 8 ) & 0xff) + "." +
((i >> 16 ) & 0xff) + "." +
( i >> 24 & 0xff) ;
}
<uses-permission android:name="android.permission.internet"></uses-permission>
public string getlocalipaddress()
{
try
{
for (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();)
{
networkinterface intf = en.nextelement();
for (enumeration<inetaddress> enumipaddr = intf.getinetaddresses(); enumipaddr.hasmoreelements();)
{
inetaddress inetaddress = enumipaddr.nextelement();
if (!inetaddress.isloopbackaddress())
{
return inetaddress.gethostaddress().tostring();
}
}
}
}
catch (socketexception ex)
{
log.e("wifipreference ipaddress", ex.tostring());
}
return null;
}
1.使用wifi
首先设置用户权限
复制代码 代码如下:
<uses-permission android:name="android.permission.access_wifi_state"></uses-permission>
<uses-permission android:name="android.permission.change_wifi_state"></uses-permission>
<uses-permission android:name="android.permission.wake_lock"></uses-permission>
其次,代码如下
复制代码 代码如下:
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
//获取wifi服务
wifimanager wifimanager = (wifimanager) getsystemservice(context.wifi_service);
//判断wifi是否开启
if (!wifimanager.iswifienabled()) {
wifimanager.setwifienabled(true);
}
wifiinfo wifiinfo = wifimanager.getconnectioninfo();
int ipaddress = wifiinfo.getipaddress();
string ip = inttoip(ipaddress);
edittext et = (edittext)findviewbyid(r.id.edittext01);
et.settext(ip);
}
private string inttoip(int i) {
return (i & 0xff ) + "." +
((i >> 8 ) & 0xff) + "." +
((i >> 16 ) & 0xff) + "." +
( i >> 24 & 0xff) ;
}
2.使用gprs
首先,设置用户上网权限
复制代码 代码如下:
<uses-permission android:name="android.permission.internet"></uses-permission>
其次,代码如下
复制代码 代码如下:
public string getlocalipaddress()
{
try
{
for (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();)
{
networkinterface intf = en.nextelement();
for (enumeration<inetaddress> enumipaddr = intf.getinetaddresses(); enumipaddr.hasmoreelements();)
{
inetaddress inetaddress = enumipaddr.nextelement();
if (!inetaddress.isloopbackaddress())
{
return inetaddress.gethostaddress().tostring();
}
}
}
}
catch (socketexception ex)
{
log.e("wifipreference ipaddress", ex.tostring());
}
return null;
}