Question

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.

Was it helpful?

Solution

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.

enter image description here

get IMEI use:

TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei_no = tm.getDeviceId(); 

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top