Friday, February 10, 2012

How to get my telephone number through code in Android?

Gets the phone number the app is running on. It needs a permission so add the line uses-permission android:name="android.permission.READ_PHONE_STATE" to the manifest.xml 

 private String getMyPhoneNumber(){
   TelephonyManager mTelephonyMgr;
   mTelephonyMgr = (TelephonyManager)
    getSystemService(Context.TELEPHONY_SERVICE); 
   return mTelephonyMgr.getLine1Number();
 }

 private String getMy10DigitPhoneNumber(){
  String s = getMyPhoneNumber();
  return s.substring(2);
 }

No comments:

Post a Comment