Showing posts with label IP address. Show all posts
Showing posts with label IP address. Show all posts

Friday, February 7, 2014

Get my IP Address

It's a example to list IP address of Android device.

my IP Address
my IP Address

package com.example.androidmyip;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView info;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  info = (TextView)findViewById(R.id.info);
  
  info.setText(getIpAddress());
  //info.setText(getLocalIpAddress());
 }
 
 private String getIpAddress(){
  String ip = "";
  try {
   Enumeration<NetworkInterface> enumNetworkInterfaces = 
     NetworkInterface.getNetworkInterfaces();
   while(enumNetworkInterfaces.hasMoreElements()){
    NetworkInterface networkInterface = 
      enumNetworkInterfaces.nextElement();
    Enumeration<InetAddress> enumInetAddress = 
      networkInterface.getInetAddresses();
    while(enumInetAddress.hasMoreElements()){
     InetAddress inetAddress = enumInetAddress.nextElement();
     
     String ipAddress = "";
     if(inetAddress.isLoopbackAddress()){
      ipAddress = "LoopbackAddress: ";
     }else if(inetAddress.isSiteLocalAddress()){
      ipAddress = "SiteLocalAddress: ";
     }else if(inetAddress.isLinkLocalAddress()){
      ipAddress = "LinkLocalAddress: ";
     }else if(inetAddress.isMulticastAddress()){
      ipAddress = "MulticastAddress: ";
     }
     ip += ipAddress + inetAddress.getHostAddress() + "\n"; 
    }
    
   }
   
  } catch (SocketException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   ip += "Something Wrong! " + e.toString() + "\n";
  }
  
  return ip;
 }

}

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Remark: <uses-permission android:name="android.permission.INTERNET"/> is needed in AndroidManifest.xml.

download filesDownload the files.

Sunday, June 5, 2011

Get IP addresses associated with the given host

java.net.InetAddress is a class of an Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and in practice you'll have an instance of either Inet4Address or Inet6Address (this class cannot be instantiated directly). Most code does not need to distinguish between the two families, and should use InetAddress.

An InetAddress may have a hostname (accessible via getHostName), but may not, depending on how the InetAddress was created.

In this exercise, we are going to get all IP addresses associated with a host, by calling the function getAllByName().

Get IP addresses associated with the given host

package com.exercise.AndroidIP;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidIP extends Activity {
 
 EditText inputHostName;
 Button btnCheck;
 TextView textInetAddress;
 
 final static String DEFAULT_HOST_NAME = "www.google.com";
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    inputHostName = (EditText)findViewById(R.id.hostname);
    btnCheck = (Button)findViewById(R.id.check);
    textInetAddress = (TextView)findViewById(R.id.InetAddress);
    
    inputHostName.setText(DEFAULT_HOST_NAME);
    btnCheck.setOnClickListener(btnCheckOnClickListener);
   }
  
   Button.OnClickListener btnCheckOnClickListener
   = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String hostName = inputHostName.getText().toString();
   try {

    InetAddress[] hostInetAddress
     = InetAddress.getAllByName(hostName);
    String all = "";
    for(int i = 0; i < hostInetAddress.length; i++){
     all = all + String.valueOf(i) + " : "
      + hostInetAddress[i].toString() + "\n";
    }
    textInetAddress.setText(all);
    
   } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidIP.this, e.toString(),
      Toast.LENGTH_LONG).show();
   }
  }
    
   };
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Host Name"
   />
<EditText
   android:id="@+id/hostname"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
   android:id="@+id/check"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Check "
   />
<TextView 
   android:id="@+id/InetAddress"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>


Grant permission of "android.permission.INTERNET"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.AndroidIP"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>

   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidIP"
                 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>


Download the files.