【Android】获取当前连接的wifi IP地址

AndroidManifest.xml文件增加用户权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kt292tree"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 
 
private String getlocalip(){ 
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
int ipAddress = wifiInfo.getIpAddress(); 
if(ipAddress==0)return "未连接wifi"; 
return ((ipAddress & 0xff)+"."+(ipAddress>>8 & 0xff)+"." 
+(ipAddress>>16 & 0xff)+"."+(ipAddress>>24 & 0xff)); 
} 

你可能感兴趣的:(【Android】获取当前连接的wifi IP地址)