2

Hi I am writing an Android app which only allows the registered phone user to use. how to get the phone number that the android app is on. Thanks

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Bill Wu
  • 29
  • 3
  • 1
    Approx 2 months ago, I also want solution of this problem and think just do it as whatsapp. But suddenly an Idea came in my mind that I have to write code my own way to confirm mobile number of user which used in my android app. So I posted question on SO, but didn't get better answer and after some time I written my own way code to get user mobile number. Here is the link of my question and just check my answer. http://stackoverflow.com/questions/15019582/how-to-check-user-entered-own-phone-number-in-edittext – Manish Dubey Jun 06 '13 at 07:17

5 Answers5

1

You can use following code to get Mobile Number

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mMobileNo =  telephonyManager.getLine1Number();

Add following permission in AndroidManifest.xml

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

Problem : It can be null

According to the documentation .getLine1Number() "Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable. "

Apparently .getLine1Number() reads this information from SIM card, so if the operator has set the MSISDN field it will return you its value and null if they did not set this field.

In your case probably your SIM card does not have this field populated by operator.

For More Info You can Visit this.

Solution

This is not the correct way to verify mobile number , you can do it like whatsapp does , you can find it from this link.

Community
  • 1
  • 1
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
0

TelephonyManager is a way to go:

TelephonyManager tm=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
number = tm.getLine1Number();

Keep in mind that it required: READ_PHONE_STATE permission.

0

Calling getLine1Number() on the TelephonyManager would do it for you.

TelephonyManager mTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
userPhoneNumber = mTelephonyManager.getLine1Number();

Excerpt from the documentation :

public String getLine1Number ()

Added in API level 1 Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.

Requires Permission: READ_PHONE_STATE

Swayam
  • 16,294
  • 14
  • 64
  • 102
0
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

with permission: READ_PHONE_STATE

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

I would not use any of the above techniques for phone number, since these api's are not consistent across all phones. Spice, Micromax and many Samsung phones do not return the phone number. Use the api's to get a good guess of the users phone number, but dont forget to prompt to allow the user to correct it.

HTC phones are much better in that way, the API works good in all HTC phones.

Siddharth
  • 9,349
  • 16
  • 86
  • 148