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" />
Related
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
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.
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.
I am using following android code to get mobile number and it's working on android emulators only:
TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String number = tm.getLine1Number();
But when I tested it in real device (Samsung Galaxy Chat B5330) it gives nothing (Empty String)
Please help with some code snippet.
For GSM, the phone number is on the SIM card, and some carriers just don't put it on the card, and then the phone does not know what it is, but in this case you
should get an empry string rather than a null
if the carrier stores the number on your SIM, go to Settings -> About Phone -> Status -> My phone Number. If it displays unknown there, then your number is not stored on the SIM.
get IMEI use:
TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei_no = tm.getDeviceId();
Add permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
See this
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();
I want to retreive mobile number of device programatically. My code is:-
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String strMobileNumber = telephonyManager.getLine1Number();
and I have set READ_PHONE_STATE permission in AndroidManifest.xml file. This is working on emulator but on actual device with eclair version, it is returning empty string. Is there any other way to get mobile no?
There is no reliable way to "retreive mobile number of device programatically", AFAIK. getLine1Number() reports the number supplied by the SIM card for GSM phones, and the mobile number does not need to be on the SIM card.