In this post, we will learn how to develop a simple android app to check IP address of your device.

Android Manifest.xml

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.thoughts.checkip”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”13″ />

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.CheckIp”
android:label=”@string/title_activity_check_ip” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

</application>

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

</manifest>

 

activity_check_ip.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
xmlns:ads=”http://schemas.android.com/apk/lib/com.google.ads”

android:layout_width=”match_parent”
android:layout_height=”match_parent” >
<Button
android:id=”@+id/chckip”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Check My IP” />

<com.google.ads.AdView android:id=”@+id/adView”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

ads:adUnitId=”a1511ca88fb60b7″

ads:adSize=”BANNER”

ads:loadAdOnCreate=”true”/>

</RelativeLayout>

 

CheckIp.java

 

package com.thoughts.checkip;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import org.apache.http.conn.util.InetAddressUtils;

import com.thoughts.checkip.R;
import com.thoughts.checkip.*;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CheckIp extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_ip);
Button b1 = (Button) findViewById(R.id.chckip);
final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);

b1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){

try {

String hostname=”amit”;
InetAddress host=InetAddress.getLocalHost();
hostname=host.getHostName();
System.out.println(“Host is:: “+hostname);

List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress().toUpperCase();
boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);

if (isIPv4)

{

Toast.makeText(getApplicationContext(), sAddr,Toast.LENGTH_LONG).show();

alt_bld.setMessage(“IP Address : “+sAddr)

.setCancelable(false)
.setPositiveButton(“OK”, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//
dialog.cancel();
}
});

AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle(“CheckIp”);
// Icon for AlertDialog
alert.setIcon(R.drawable.ic_launcher);
alert.show();

}

}
}
}
} catch (Exception ex) { } // for now eat exceptions

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_check_ip, menu);
return true;
}
}