2012年10月4日 星期四

Android : How to get gateway and subnet mask details in Android?


from
http://stackoverflow.com/questions/5387036/how-to-get-gateway-and-subnet-mask-details-in-android-programmatically

這個需要 2.3以上 http://www.programmer-club.com/ShowSameTitleN/java/13615.html


這種資訊最完整



全區域變數宣告

  public String   s_dns1 ;
   public String   s_dns2;  
   public String   s_gateway;
   public String   s_ipAddress;  
   public String   s_leaseDuration;  
   public String   s_netmask;
   public String   s_serverAddress;
    DhcpInfo d;
    WifiManager wifii;
 

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
       d=wifii.getDhcpInfo();

       List_IP.add("DNS 1: "+intToIp(d.dns1));
       List_IP.add("DNS 2: "+intToIp(d.dns2));  
       List_IP.add("Default Gateway: "+intToIp(d.gateway));  
       List_IP.add("IP Address: "+intToIp(d.ipAddress));
       List_IP.add("Lease Time: "+intToIp(d.leaseDuration));  
       List_IP.add("Subnet Mask: "+intToIp(d.netmask));  
       List_IP.add("Server IP: "+intToIp(d.serverAddress));


     public String intToIp(int i) {
      return ( i & 0xFF) + "." +
      ((i >> 8 ) & 0xFF) + "." +
           ((i >> 16 ) & 0xFF) + "." +
           ((i >> 24 ) & 0xFF );
    }





=====================================================
或是這種 簡易一點



InetAddress myIP = null;
try{
 
Log.i("1~~~",java.net.InetAddress.getLocalHost().getHostAddress());
 
 
   Enumeration ifE=NetworkInterface.getNetworkInterfaces();
   while(ifE.hasMoreElements()){
Enumeration addrE=((NetworkInterface)ifE.nextElement()).getInetAddresses();
while(addrE.hasMoreElements())
{

Log.i("2~~~","addrs:"+((InetAddress)addrE.nextElement()).getHostAddress());
}
   }

 
 }catch( Exception e) {
 Log.i("3~~~",e+"");
 }




更簡單點


        InetAddress myComputer = null;

try {
myComputer = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        //System.out.println(myComputer.getHostAddress());

0 意見:

張貼留言

 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template