2015-04-23 166 views
0

我有一個問題我要得到我的移動IP的Android如何讓網絡/無線網絡連接的IP

是否是網絡或Wi-Fi

我嘗試了一些辦法,但我不知道爲什麼我不能讓它

public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getLocalIpAddress(); 
    } 
    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()){ 
            Log.e("IP", inetAddress.getHostAddress().toString()); 
            return inetAddress.getHostAddress().toString(); 
           } 
        } 
      } 
     } catch (SocketException ex) { 
      Log.e("Error:" , ex.toString()); 
      } 
     return null ; 
     } 
} 

這是我的日誌

fe80::a3c8:4c03:154:8e1d%rmnet_usb0 

這是我AndroidManifest

<uses-permission android:name= "android.permission.INTERNET" /> 
    <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" /> 
+0

可能重複[獲取我的無線IP地址的Android(http://stackoverflow.com/questions/16730711/get-my -wifi-ip-address-android) –

+0

@PiotrGolinski是這樣可以gey的wifi地址,但我不能得到3G IP –

回答

0
Use this for getting device ip address: 
WifiManager wm = (WifiManager) this.getSystemService(this.WIFI_SERVICE); 
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress()); 
+0

這種方式可以得到wifi,但我想得到3G地址 –

+0

PLZ試試這可能會有所幫助喲ü? –

0

你可以試試這個:

private String getIPAddress(Context context) { 

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    int ip = wifiInfo.getIpAddress(); 
    return Formatter.formatIpAddress(ip); 
} 
+0

這種方式可以得到wifi,但我想獲得3G地址 –

1
Plz try this it may help you..? 
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 (Exception ex) { 
       Log.e("IP Address", ex.toString()); 
      } 
      return null; 
     } 

Add below permission in the manifest file. 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+0

這種方式將得到味精:fe80 :: a3c8:4c03:154:8e1d%rmnet_usb0 –

+0

Plz去參考這個http://www.technocrazed.com/android-find-and-display-ip-address-of-a - 移動 - 編程 –