Search This Blog

Saturday, 2 July 2011

How to get Phone Number in Android application..???

import android.app.Dialog;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;


public class ClsGetPhoneNumber_UI {
 Dialog dialog;
 String strPHNo;
 StringBuffer sb;

 public String getMyPhoneNumber(Context context){
   
    TelephonyManager mTelephonyMgr;
       mTelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
       try
       {
        sb=new StringBuffer(mTelephonyMgr.getLine1Number());
        System.out.println("Sb"+sb.toString());
      
        if(sb==null)
        {
         strPHNo= "NOPHONE";
         System.out.println("strPHNo1"+strPHNo);
        }
        else if(sb.toString().equals(""))
        {
         return "NOPHONE";
        }
        else
        {
         sb.reverse().setLength(10);
         String ReverseNumber=sb.toString().trim();
         strPHNo= ReverseNumber;
         System.out.println("strPHNo2"+strPHNo);
        }
       }
       catch(Exception e)
       {
        System.out.println("strPHNo3"+strPHNo);
        return "NOPHONE";
       }
    return strPHNo;
 }
}

It will return Phone Number. If it will not able to get phone number then it will return NOPHONE.