Get phone number ANDROID - android

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.

Related

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.

Getting the number of the phone Xamarin.Android?

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

Getting SIM serial number in Android

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.

Getting phone number from android mobile phone

Which commands should I use in order to get the sim phone number from an Android mobile phone?
Exploring the sample of phone dialer i found "PhoneDialerService.GetCarrier" but with no option for getting mobile's phone number.
If the device uses only one sim card use :
TelephonyManager phoneManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
In case the device uses two or more sim cards, use:
TelephonyManager phoneManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String simSerialNumber = phoneManager.getSimSerialNumber();
Don't forget the permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Note that there isn't any guaranteed solution. This won't work(might return null) on every device/country because phone numbers are not physically stored on all SIM cards. The best solution is to ask the user to enter his/her phone number once and store it, and ask if number is ported/changed.

Not able to get the SIM number when I use Airtel provider SIM card in Android?

I am working in my application and fetching phone number using the method:
TelephonyManager phoneManager = (TelephonyManager)appContext.getSystemService(Context.TELEPHONY_SERVICE);
number = phoneManager.getLine1Number();
It is working well and good with all the SIM card, but getting problem in Airtel provider SIM card.
I am getting blank while trying with this SIM card.
I don't know why Android API is dealing differently with different providers. Please suggest me.
It is because the MSISDN number is not filled on the SIM card by the operator. unfortunately nothing you can do about that.
Thread with some insight on it here
You have to use Telephony Manager;If at all you not found the contact no. of user; You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...
Add permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Import:
import android.telephony.TelephonyManager;
Use the below code:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
imei = tm.getDeviceId();
// get SimSerialNumber
simSerialNumber = tm.getSimSerialNumber();

Categories

Resources