3

I want to get mobile number of own mobile.

I searched android telephony class but i can not find any useful function over there.

thanks in advance for your help.

rajpara
  • 5,203
  • 1
  • 29
  • 46

3 Answers3

6

Can do this:

TelephonyManager mTelephonyMgr;  
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);   

String yourNumber = mTelephonyMgr.getLine1Number();  

Add Permission to Manifest file:

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

xil3
  • 16,305
  • 8
  • 63
  • 97
  • 1
    thanks xil3 : getLine1Number() return null in my application. :( sim card is already inserted in mobile. – rajpara Feb 04 '11 at 10:14
  • Does the phone let you dial out? Is the SIM card working? – xil3 Feb 04 '11 at 10:15
  • Also, did you set this permission: `android.permission.READ_PHONE_STATE` – xil3 Feb 04 '11 at 10:16
  • i think first we register that number into nework. – Pinki Feb 04 '11 at 10:19
  • The device needs to know what the number is first, before it can tell you it. If it hasn't registered to the network with the SIM, then it won't know. – xil3 Feb 04 '11 at 10:22
  • i test this code on working mobile , i already set permission : android.permission.READ_PHONE_STAT , i can make call to other mobile and receiving - so its meaning is that my number already register to network – rajpara Feb 04 '11 at 10:38
  • guys i test this sample code in my running mobile so there is no possibility of network or sim card. Number is working fine , just need to find way for fetching own mobile number. – rajpara Feb 04 '11 at 10:41
  • Why did I get a down-vote? This is how you get the number. If your device doesn't know it's own number, then it'll return null. Nothing you can do about that. – xil3 Feb 04 '11 at 10:58
  • In the case of MSISDN number is null that time we get null otherwise we easily get number. in most of cases MSISDN number is same as mobile number but in my second mobile MSISDN number is old sim mobile number and now i am using new sim card so i am not able to get latest mobile number. :( – rajpara Feb 04 '11 at 11:26
  • Hello guys, by using getLine1Number(); it return null in one phone and blank in other mobile, is there any solution for getting mobile number in android ? thanks – Jayesh Nov 25 '11 at 10:02
  • UPDATE- This Answer is too old and will not work for most of the cases, Please don't use any method to fetch mobile number programatically, instead of that please try to enter it by the user itself. – Sudhanshu Gaur Jun 16 '16 at 13:49
3

I think there are cases where the phone does not know its own number - it somehow depends on the network provider / SIM card.

I do have one SIM card where it works, another one (different provider, prepaid) where it does not work.

sstn
  • 3,050
  • 19
  • 32
  • okie , then i have to check this with different provider , let me try this – rajpara Feb 04 '11 at 10:43
  • i found out " getLine1Number() return MSISDN number which is listed in Setting->About Phone" that number is mobile number. in my one mobile it display MSISDN number none thats why null is returned from getLine1Number(). surprisingly in my second mobile MSISDN number listed there but it is old mobile number not latest one because i changed my sim card and now its different number from listed one. – rajpara Feb 04 '11 at 11:23
  • Exactly, regarding the card which does not return a number via getLine1Number(), the phone displays "Phone number: unknown". – sstn Feb 04 '11 at 13:29
2

Try the following code:

TelephonyManager mTelephonyMgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

String sDeviceID = mTelephonyMgr.getDeviceId();
String sSimSerial = mTelephonyMgr.getSimSerialNumber(); 

Set the following permission

android.permission.READ_PHONE_STATE in Android Manifest file
chiranjib
  • 5,288
  • 8
  • 53
  • 82