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);
}
4 Comments
Not allways working
Number that we use here is 10 digits long and i get only 8 ! Anyone a solution for this ?
mTelephonyMgr.getLine1Number() throwing always null. Is there other way to find out phone number programatically.
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.