Tutorial :Is there a way to retrieve the user's phone number in an mobile app?



Question:

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.


Solution:1

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.)


Solution:2

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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »