I used this code for fetching the number, But it gave me null
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
It may not always return mobile number.
Let me explain how this actually works: When you buy a new SIM card for a new number, the provider had already (though not always) put a mobile number in the SIM (SIM Cards do have a little memory). Now TelephonyManager API tries to read that number. If it is available then it returns the number.
Now, you can get a SIM without a mobile number associated with it. Example - new SIM for porting (where you use previous number in new SIM Card). Those SIMs don't have any number stored on them, and hence TelephonyManager returns null.
Now, even if TelephonyManager returns a number, you should not trust it to be right. Because many devices provide option to edit that number.
To use getLine1Number() you need either READ_PHONE_STATE OR READ_SMS permission. You may be missing that.
And remember that it returns null if phone number string is unavailable.
Related
I need to make sure the SIM card can be used without entering pin in designated device(s), but it must be required in other devices. (Devices in use are rooted, application is installed as system app)
What I want is to identify a SIM card, and enter the pin programmatically, if one is already known/saved.
I am able to enter the known pin code, when one is required, but I have a problem with SIM identification.
I found out that I am not able to get SIM serial number or subscriber id before the correct PIN is entered.
The method I am using (with permissions and etc.):
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getSimSerialNumber();
The thing is, this method returns null, if pin is required and not yet entered.
My question is if there is some kind of workaround for this.
Is it possible at all?
Is there another approach?
Ideas and approaches appreciated.
Thanks in advance.
TelephonyManager tm = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
}
String simID = tm.getSimSerialNumber();
String telNumber = tm.getLine1Number();
String IMEI = tm.getDeviceId();
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Your issue:
The phone number returned is whatever is stored on the SIM card's MSISDN, which is not filled by some operators. Try Airtel, Vodafone if in India.
I tried getLine1number(), for some device it gets the number. but for some, it returns empty string("").
As far as I tested, when in the device's Settings > Status > My Phone number is not empty, I get the number otherwise I am not able to get the Phone number.
Is there any other way to get the Phone number other than the above method?
As per my knowledge their is no any way to find a telephone number other than .getLine1Number() method.
Provide you some options that you try:-
A) If number return null means, your phone number not register in your mobile.For that you can use the sim card serial number.It is unique number.
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
B) The method you are using is the only one part of the SDK to do this,
and only works on devices where the number is stored on the SIM card,
which only some carriers do. For all other carriers,
you will have to ask the user to enter the phone number manually,
as the number is simply not stored anywhere on the device from where you can retrieve it.
Hope it will helps you.
I am developing an application that I need to find user phone number.
I founded Telephony Manager but this class does not guarantee that the phone number always returns.so i want to know is there any solution(probably not a straight solution that can i give users phone number because the number does not coded on sim card) in android?
Only few service providers provide phone number via getLine1Number() method. Instead you can get a unique number for each sim using getSimSerialNumber() method.
Ask the user to enter phone number, you can store his simnumber using getSimSerialNumber();
Then authenticate depending on the sim number, this should solve your problem
You don't have much possibilities here you can use either :
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Which as you said is not guaranteed to always return a number. Or you can ask user to enter the number manually. I think there's no other option.
The following code does not give me the phone number of the device ,how can i get the phone number
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
Toast.makeText(getApplicationContext(),"Telephone number: "+ number,
Toast.LENGTH_SHORT).show();
ALso i have tried the following..but the number doest show up
Programmatically obtain the phone number of the Android phone
How to get the mobile number of current sim card in real device?
The only method available from Android API as all the comments suggest is getLine1Number(). However I have never been able to obtain it since your operator must provide you with a SIM that supports letting phone read internal phone number. That seems not very common, so I am afraid that you could be left without means of knowing it.
That's why some programs that use your phone number to identify you (i.e. Whatsapp) do ask user about his/her phone number, because there is no sure way to getting it programatically.
the code provided:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
should work fine, however, it will not if you forgot the permission "READ_PHONE_STATE".
Hope this works now :)
Try this:
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Hope it help you
[In case if anyone still stumbles here looking for this still existing age-old problem]
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimised with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
I use the TelephonyManager to do this and you can access TelephonyManager s methods as given below
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = telephonyManager.getLine1Number();
The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be available.
I have also given application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
The number is always blank or null...
While I have also crosschecked calling and i'm able to call from that number(i.e inserted sim no)
I have gone through numerous documentations but nothing help me out to get the phone number from sim so as to be used in my app..
With Regards,
Arpit Garg
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
you should try this
TelephonyManager phoneManager = (TelephonyManager)
getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();