I found out that this code shared by some users doesn't work in my project. Has anyone a better solution ?
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
I'm getting an error on the getLine1Number() and on the mAppContext
Your code is for native Android (Java). Try the below code of Xamarin.Android (c#).
Code:
Android.Telephony.TelephonyManager tMgr = (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);
string mPhoneNumber = tMgr.Line1Number;
Required Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
[Edited]
Sometimes Line1Number Property will return null if the number is unavailable, but it does not say when the number might be unavailable.
These are occured due to the reason that the phone number not register in your mobile, or according to some restrictions created by the device manufactures.
From Documentation :
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
So you have done everything right, but there is no phone number stored.
If you get null, you could display something to get the user to input the phone number on his/her own.
Did you add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> to manifest?
It's not really perfect, but the only way (at least to my knowledge). Broader discussion is here Programmatically obtain the phone number of the Android phone
Related
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.
I want get a phone number, but in device I get "" (empty). Any suggest?
This is my code:
TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
mPhoneNumber is empty.
Manifest has:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Some models return the phone number, some don't. I had the need for getting the phone number and ended up using Twitter's Fabric Digits. Easy to setup and free to use.
That API call is probing your sim card. Some telecom companies don't require the population of the phone number field in order to operate. So if your sim card doesn't have that information your string will be blank each time.
I have tried below code
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I have tried in two device
Micromax Canvas-4 (Getting user phone number)
Htc desire 816G (Not able to get user phone number)
Please help me !!
Your help will be appreciated. Thanks in advance.
It is occurring because you need to set your own phone number on the device.
please have a look at the below link.
getLine1Number() Returns blank, not null
This will solve your problem.
I have used the standard code below to fetch the mobile number on my ANDROID 2.2 GSM device, BUT I ALWAYS GET AN EMPTY STRING. Is this feature supported on 2.2?
Any suggestion as to how I can fetch the mobile number on Android 2.2 device.
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Go to Settings -> About phone -> Status -> see at My Phone Number field. If it says "Unknown" then it's not your issue, it's just the fact that some carriers don't pack the SIM cards with own number. On some devices user can edit own number, but it's not always supported by the firmware.
Read here
That should work, but make sure you're requesting the READ_PHONE_STATE permission in your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
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();