Wednesday, September 22, 2010

Get IP address of device in android

AIM :Get IP address of device in android

The following method returns the device ip address which is currently used by device irrespective of type of connection ie.,either 3g/wifi/any other.

It returns NULL if there is no network connection.

public static String getipAddress() {
            try {
                for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            String ipaddress=inetAddress.getHostAddress().toString();
                            Log.e("ip address",""+ipaddress);
                            return ipaddress;
            } catch (SocketException ex) {
                Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
            return null;

Call the above method wherever you wnat to get ip address as
String ipaddress=getipAddress();


Tuesday, September 7, 2010

Vibrate ,Lock the screen,getPhonenumber in android

Vibrate the Phone

You can vibrate the phone for a specified duration like so: 
(Vibrator) getSystemService(Context.VIBRATOR_SERVICE).vibrate(milliseconds);

You can use the AudioManager to enable and disable silent mode. 
mAudio = (AudioManager) getSystemService(Activity.AUDIO_SERVICE);

 to lock the screen in an Android app?

Remember you need to give permissions for it:
and add these lines to code.
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

How to get my telephone number?

add the line uses-permission android:name="android.permission.READ_PHONE_STATE" to the manifest.xml 
 TelephonyManager mTelephonyMgr;
 mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
 String phonenumber= mTelephonyMgr.getLine1Number();

How to hide the title bar(FULL SCREEN MODE ) ?

Add this lines in your code in oncreate() method just before setting setContentView() method.

 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN); 


Notification in Android

AIM :  Notification in Android.


 NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.icon,"Notify", System.currentTimeMillis());

notification.setLatestEventInfo(getApplicationContext(), "Title", "Description", null);

mManager.notify(NOTIFICATION_ID, notification);; 

 To cancel  the same notification,call 


Turn off / on wifi in android using code

AIM:  To turn on and turn off wifi in android. 

  •  Declare the following in your manifest file. 
< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" / > < uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/ > < uses-permission android:name="android.permission.CHANGE_WIFI_STATE" / > < uses-permission android:name="android.permission.WAKE_LOCK" / >

  • In  your Activity class
private WifiManager wifiManager;
public void onCreate(Bundle icicle) { 
       // Get the Wifi service from our system
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

  // Check the our wifi is currently turned on or turned off  
    wifiManager.setWifiEnabled(false); // Turn on/off our wifi

Android Developers Blog

Ram's shared items

Amazon Product Ads