41

How can I get my phone number in Android? When I use:

TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

and use:

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

it returns null, why?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • possible duplicate of [Programmatically obtain the phone number of the Android phone](http://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone) – bluish Dec 09 '14 at 09:53

6 Answers6

23

From the 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.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • You can't get the phone number in any other way if it is not stored on the SIM card or the device itself. – Ben Weiss Sep 19 '13 at 12:16
11

If the function you called returns null, it means your phone number is not registered in your contact list.

If instead of the phone number you just need an unique number, you may use the sim card's serial number:

    TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String getSimSerialNumber = telemamanger.getSimSerialNumber();  
Ram
  • 2,532
  • 4
  • 22
  • 25
  • 2
    And what do you do with that? Is there any way to get a phone number using SIM card serial number? – Pere Sep 20 '15 at 21:43
  • What do you mean that "Phone no is not registered in your mobile"? How to register phone no in mobile? please explain. – Ashish Tiwari Apr 28 '16 at 07:15
9

As Answered here

Use below code :

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

In AndroidManifest.xml, give the following permission:

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

But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.

Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!

Suggested Workaround:

  1. Get the user's phone number as manual input from the user.
  2. Send a code to the user's mobile number via SMS.
  3. Ask user to enter the code to confirm the phone number.
  4. Save the number in sharedpreference.

Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.

Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
2
private String getMyPhoneNumber(){
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager)
        getSystemService(Context.TELEPHONY_SERVICE); 
    return mTelephonyMgr.getLine1Number();
}

private String getMy10DigitPhoneNumber(){
    String s = getMyPhoneNumber();
    return s.substring(2);
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Robi Kumar Tomar
  • 3,418
  • 3
  • 20
  • 27
  • what is the difference b/w this answer and op question ? – chhameed Jan 21 '14 at 12:11
  • @Robi your code isn't helpful because if you do "return mTelephonyMgr.getLine1Number();" could return null if number is not stored on telephone so in the next method "return s.substring(2);" The code will break.... – Rafael Ruiz Tabares Apr 29 '14 at 15:38
1

Method 1:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

With below permission

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

Method 2:

There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.

Try below code:

String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
        main_data, "mimetype=?",
        new String[]{"vnd.android.cursor.item/phone_v2"},
        "is_primary DESC");
if (object != null) {
    do {
        if (!((Cursor) (object)).moveToNext())
            break;
        String s1 = ((Cursor) (object)).getString(4);
    } while (true);
    ((Cursor) (object)).close();
}

You will need to add these two permissions.

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

Hope this helps, Thanks!

activesince93
  • 1,716
  • 17
  • 37
0

Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.

editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
TelephonyManager tMgr;
tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
if (mPhoneNumber != null){
editTextPhoneNumber.setText(mPhoneNumber);
}