2

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.

Arun C
  • 9,035
  • 2
  • 28
  • 42
Parmanand
  • 355
  • 2
  • 6
  • 15
  • Probably have to use USSD code from the local carrier, not sure but am also interested to know! –  May 02 '13 at 08:51
  • 1
    It depends on the SIM card. You can find a useful answer here : http://stackoverflow.com/a/5134725/2198638. – Brtle May 02 '13 at 08:55

3 Answers3

5

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(); 
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • Ya its showing unknown....is there any other way to get mobile number (by using sim card number or IMEI)?? – Parmanand May 02 '13 at 08:57
  • 3
    i dont think there is other way.. because its all depend on mobile carriers company(like vodaphone...etc) mean they don't give us mobile number then how we get it.. – Dhaval Parmar May 02 '13 at 09:01
1

Add permission

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

See this

Community
  • 1
  • 1
Arun C
  • 9,035
  • 2
  • 28
  • 42
1

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();
Yasar Khan
  • 71
  • 12