I want to know user mobile number in Android. I used this code but I'm not getting number.
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String n = tm.getLine1Number();
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Is it compulsory to save number in android mobile settings --> about phone --> status --> myphone number
Any idea on this?
Requires the
READ_PHONE_STATE
permission.I think sim serial Number and sim number is unique. You can try this for get sim serial number and get sim number and Don't forget to add permission in manifest file.
And add below permission into your
Androidmanifest.xml
file.Let me know if there is any issue.
You can use the TelephonyManager to do this:
Have you used
Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.
=================================================================================
Its been almost 6 months and I believe I should update this with an alternative solution you might want to consider.
As of today, you can rely on another big application Whatsapp, using AccountManager. Millions of devices have this application installed and if you can't get the phone number via TelephonyManager, you may give this a shot.
Permission:
Code:
Check actype for whatsapp account
Of course you may not get it if user did not install Whatsapp, but its worth to try anyway. And remember you should always ask user for confirmation.
Getting the Phone Number, IMEI, and SIM Card ID
For SIM card, use the getSimSerialNumber()
Phone number of your phone, use the getLine1Number() (some device's dont return the phone number)
IMEI number of the phone, use the getDeviceId()
Permissions needed
You have everything right, but the problem is with
getLine1Number()
function.this method works only for few cell phone but not all phones.
So, if you need to perform operations according to the sim(other than calling), then you should use
getSimSerialNumber()
. It is always unique, valid and it always exists.