How to Get Device IP Address In Android
This psot about how to get device ip address.
Below i mention the code and follow it.
Activity Code
Declare Permission in Manifest
This psot about how to get device ip address.
Below i mention the code and follow it.
Activity Code
package
com.example.deviceipaddress;
import
android.app.Activity;
import
android.net.wifi.WifiInfo;
import
android.net.wifi.WifiManager;
import
android.os.Bundle;
import
android.widget.TextView;
publicclass
MainActivity extends Activity {
@Override
protectedvoid
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WifiManager wifiManager =
(WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo =
wifiManager.getConnectionInfo();
int ipAddress
= wifiInfo.getIpAddress();
String ipAddresStr =
integerToIp(ipAddress);
TextView txt = new TextView(this);
txt.setText("IP
Address : " + ipAddresStr);
txt.setTextSize(20);
setContentView(txt);
}
// Convert Integer to IP
public String
integerToIp(int i) {
return ((i
>> 24) & 0xFF) + "." + ((i >> 16)
& 0xFF) + "."
+ ((i >> 8)
& 0xFF) + "." + (i & 0xFF);
}
}
Screen Shot
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deviceipaddress"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="com.example.deviceipaddress.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>