Very often app developers need to find the IP address of the Android device on which an app is installed. This post explains how you can find and display the IP address of a device programmatically. You can download complete source code of the project here.
You may be connected to internet through a Wifi or a cellular network. You can get the IP address of your mobile device programmatically using the following steps:
- First of all, find and list all the different network interfaces in your mobile
- Find their IP addresses
- Then one by one check each if each IP address is not a loopback address. Loopback address is only used for testing purposes and does not has an associated hardware to connect to the internet.
- If an IP address is not a loopback address, then its an IP address with which you are connected to internet.
Now, we move to the implementation phase:
1. Create a new project in android by going to File-> New Project, and completing all the required steps.
2. Change the main layout .xml file (activity_main.xml) as:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#ff000000" tools:context="com.example.getdeviceip.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#ffff0000" android:textSize="40dp" android:text="TextView" /> </RelativeLayout>
3. Change your main activity .java file (MainActivity.java) file as:
package com.example.getdeviceip; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.textView1); //Display the IP address, obtained from function getIPAddress, using textview tv.setText(getIPAddress()); } public String getIPAddress() { try { //Enumerate all the network interfaces for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); // Make a loop on the number of IP addresses related to each Network Interface for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); //Check if the IP address is not a loopback address, in that case it is //the IP address of your mobile device if (!inetAddress.isLoopbackAddress()) return inetAddress.getHostAddress(); } } } catch (SocketException e) { e.printStackTrace(); } return null; } }
3. Do not forget to add the permission to use internet in the AndroidManifest.xml. Otherwise, your code will through socket exception.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.getdeviceip" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
5. Now, building and running the project. You get this output