Is there a way to retrieve the user's phone number in an mobile app?
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.
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.)
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.
- Let user enter his mobile number. Suppose he enters 123456789
- Send a sms to this number on any port (not 0). E.g. send a sms to 123456789 on port 5001.
- for blackberry
Start a background message listener thread
For nokia, samsung, etc.
register a dynamic push registry on port 5001. - When you receive a message like. Sender’s number = receiver's number = what user has given his number.
- User’s number is verified.
精彩评论