跳至主要内容

Android获取WIFI网关地址与子网掩码地址

  1. WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);  
  2. DhcpInfo di = wm.getDhcpInfo();  
  3. long getewayIpL=di.gateway;  
  4. String getwayIpS=long2ip(getewayIpL);//网关地址  
  5. long netmaskIpL=di.netmask;  
  6. String netmaskIpS=long2ip(netmaskIpL);//子网掩码地址  
  7.   
  8. String long2ip(long ip){  
  9.     StringBuffer sb=new StringBuffer();  
  10.     sb.append(String.valueOf((int)(ip&0xff)));  
  11.     sb.append('.');  
  12.     sb.append(String.valueOf((int)((ip>>8)&0xff)));  
  13.     sb.append('.');  
  14.     sb.append(String.valueOf((int)((ip>>16)&0xff)));  
  15.     sb.append('.');  
  16.     sb.append(String.valueOf((int)((ip>>24)&0xff)));  
  17.     return sb.toString();  
  18. }  
  19.   
  20. //注意添加权限  
  21. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  




Broadcast address:

public static String getBroadcastAddr() {
    DhcpInfo di = wm.getDhcpInfo();    return long2ip((di.ipAddress&di.netmask)|~di.netmask);}
private static String long2ip(long ip){
    StringBuffer sb=new StringBuffer();    sb.append(String.valueOf((int)(ip&0xff)));    sb.append('.');    sb.append(String.valueOf((int)((ip>>8)&0xff)));    sb.append('.');    sb.append(String.valueOf((int)((ip>>16)&0xff)));    sb.append('.');    sb.append(String.valueOf((int)((ip>>24)&0xff)));    return sb.toString();
}

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.