Get My Phone Number

1 vote · 4 comments

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

raw ·
· download
private String getMyPhoneNumber(){ TelephonyManager mTelephonyMgr; mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); return mTelephonyMgr.getLine1Number(); } private String getMy10DigitPhoneNumber(){ String s = getMyPhoneNumber(); return s.substring(2); }
Add a comment


Not allways working

Reply · April 24, 2011, 2:39 a.m.

Number that we use here is 10 digits long and i get only 8 ! Anyone a solution for this ?

Reply · April 25, 2011, 4:47 a.m.

mTelephonyMgr.getLine1Number() throwing always null. Is there other way to find out phone number programatically.

Reply · March 28, 2014, 6:30 a.m.

This call SHOULD return the phone number, BUT some manufacturer decided to return null to prevent data mining from ads provider.

I recommend to ask the user to introduce it, then validate it with a code send by sms.

Reply · April 9, 2014, 10:19 a.m.