0

I want to get mobile number of own mobile, for add this part to my application .

I'm searching about this question ,

code is correct on emulator but in device show empty.

public String getMyPhoneNumber()
    {
        return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
                .getLine1Number();
    }

and :

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

But is it returning empty value. So How can I fetch the phone number detail ?

Vigbyor
  • 2,568
  • 4
  • 21
  • 35
user2991808
  • 3
  • 1
  • 3

4 Answers4

1

The method you are using is the only one part of the SDK to do this, and only works on devices where the number is stored on the SIM card, which only some carriers do. For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.

iZBasit
  • 1,314
  • 1
  • 15
  • 30
1

Answer from: Programmatically obtain the phone number of the Android phone

"There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).

I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it."

Community
  • 1
  • 1
Pontus Backlund
  • 1,017
  • 1
  • 10
  • 17
0

Your code should be fine, but you need the SIM card to be inserted in the phone. Also, as autocrat noted, if the number is not stored on sim card, you will get an empty string.

EDIT: Also, please check the link provided by Pontus Backlund, there's some good info there.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
-1

Go to setting -> about device -> status -> my phone number if my phone number is unknown u won't get mobile number.. because some of the mobile network won't give mobile number..

if my mobile number has number u can get mobile through following code

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    phoneNumber = tManager.getLine1Number();
MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
suresh
  • 414
  • 3
  • 11