views:

107

answers:

2

Is there a way to retrieve the user's phone number in an mobile app? I'm looking to build some sort of addressbook utility that builds phone numbers and let them dial the numers etc by making use of their current phone number. Is there a way to retrieve this information?

I've found some ways to retrieve it in blackberry and android from the sim card, but apparently the value in the sim card does not guarantee that its actually their number.

+1  A: 

You can use the TelephonyManager to do this:

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

The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.

You'll need to give your application permission to make this query by adding the following to your Manifest:

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

(You shouldn't use TelephonyManager.getDefault() to get the TelephonyManager as that is a private undocumented API call and may change in future.)

Pentium10
The number is likely to be unavailable if flight mode is on, or the SIM is locked. Also, not all SIMs have this information available, and if you've ported your number it will not necessarily be accurate.
Christopher
+2  A: 

For blackberry you can use

Phone.getDevicePhoneNumber(false)

According to javadoc , this method can return null if no phone number is currently available

There is one general workaround for getting user's mobile number.

  1. Let user enter his mobile number. Suppose he enters 123456789
  2. Send a sms to this number on any port (not 0). E.g. send a sms to 123456789 on port 5001.
  3. for blackberry
    Start a background message listener thread
    For nokia, samsung, etc.
    register a dynamic push registry on port 5001.
  4. When you receive a message like. Sender’s number = receiver's number = what user has given his number.
  5. User’s number is verified.
Vivart
Nice verification method!
funkybro
Can you explain what you mean by sending sms to a number on a port? You mean actually send an sms? Doesn't that cost money for the user?
erotsppa
yeah i mean actually send an SMS but it will be just one time verification cost.
Vivart
Make sure you wrap your Phone.getDevicePhoneNumber(false) with a try/catch for ControlledAccessException in case your app doesn't have permission to look this up.
Marc Novakowski