get phone number from sim card with telephonymanager - android

I want to know that how I can get actual phone number from sim card instead of sim serial number.I have also tried (manager.getLine1Number) but it is giving me null.

Ref: How to get the mobile number of current sim card in real device?
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.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 unavailable.
You'll need to give your application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(You shouldn't use TelephonyManager.getDefault() to get the TelephonyManager as that is a private undocumented API call and may change in future.)
OR
Ref: TelephonyManager give me null
> getLine1Number(); returns whatever is stored on the SIM card's MSISDN,
> which isn't filled by some operators.
In your case i am pretty much sure The MSISDN is unfilled.
You should keep one thing in mind.
getLine1Number() sometimes return with null value or with the phone number originally stored in SIM card.

A null reference might indicate that the phone number can not be accessed.
Documentation:
Note that access to some telephony information is permission-protected. Your application cannot access the protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the the methods through which you access the protected information.
Required permissions:
READ_PHONE_STATE, READ_SMS, READ_PHONE_NUMBERS link to method doc
I have also found another question facing a quite similar issue "not working" with the method you named. Someone mentioned a duplicate question there, too. This could also be helpful:
Link
If the code there is not helping you, I would recommend checking the exact documentation of your method to find details, why the null reference has been returned.

Related

Sim Management setting Programmatically in android

I want to read which sim card allocated for default voice call in sim management settings programmatically .Is that possible? I have tried to open setting provider by intent but I cant able read the sim slots allocated for calls
I dont think its possible. But hey, if you want to know which sim was used for making call, you can get it from call logs database. There is a column for sim slots in the table.
This isn't a definite answer, I'm also looking for this function.
But I managed to find a solution which works partially. You can use the TelephonyManager to obtain the preferred SIM.
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String activeSIMNetwork = tm.getNetworkOperator();
The catch here, is that this preference is not sometimes valid. If you change from one sim to another and check, it will give you the correct preferred SIM set in the phone. But if you reboot the phone and check, it will not give you the correctly displayed preference.
I will post an update if I get it to work in some other way.

how to Obtain mobile number android

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.

Retrieving phone number from SIM card error

This is my code
TelephonyManager mTelephonyMgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String sDeviceID = mTelephonyMgr.getDeviceId();
String sSimSerial = mTelephonyMgr.getSimSerialNumber();
String sSimlineNumber = mTelephonyMgr.getLine1Number();
with the help of this i want to read user phone number from his/her SIM card i also add READ_PHONE_STATE permission in manifest file. Eclipse showing no error in my code but still i get nothing in the end. Is there anything wrong with my code?
See this thread for more information on this issue. Specifically, the below comment on the accepted answer:
Actually, not so perfect. Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. It probably still does, as the Settings app still shows that defunct number. Also, there are reports that some SIMs cause this method to return null. That being said, I'm not aware of a better answer. – CommonsWare
It seems that getting a user's phone number is not as simple as asking the OS to hand it over, for reasons beyond the control of the developer. An alternative is to populate whichever field you need with the number the OS gives, but also let the user edit the phone number in case the OS is wrong.

Android phone number

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.

Why can't the TelephonyManager get the phone number from the SIM?

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();

Categories

Resources