-8

How can I build android application be able to access to the user's phone number when you download this application then i retrieve this number ?
For Example:

 public class Phonenumber  
 {
      //functions 
 } 
Chirag
  • 56,621
  • 29
  • 151
  • 198

3 Answers3

4

It is very simple to get user phone number .

TelephonyManager tMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Required

permission READ_PHONE_STATE in android manifest file.
Chirag
  • 56,621
  • 29
  • 151
  • 198
1
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);      

String mPhoneNumber = tMgr.getLine1Number();

Requires permission READ_PHONE_STATE.

Add below part in android manifest xml file.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Bishan
  • 15,211
  • 52
  • 164
  • 258
  • hi This Function didn't work ,,, You can help me – Ibrahem Al-betar Mar 12 '13 at 12:45
  • what you have tried ? update question with your code. – Bishan Mar 12 '13 at 16:09
  • Hi, unfortunality not all Carrier are saving the own phone number on the sim card. The carrier O2 e.g don`t do that. So i can`t get the own phone number with the TelephonyManager.getLine1Number() Value in any case ! Is there any other possibility to find out the own phone number in really any case ? I thought about an USSD command, e.g. *#62# to receive the answer from the carrier, but unfortunality Android dosn`t have an API for USSD So, it would be very nice if somebody give me a hint to solve this problem ! Thanx a lot and withe best regards – Ibrahem Al-betar Mar 14 '13 at 12:35
  • `Intent dial = new Intent(); dial.setAction("android.intent.action.DIAL"); dial.setData(Uri.parse("tel:"+dial_number)); startActivity(dial);` you can use this to execute USSD commands. in your case `dial_number` is ***#62#**. – Bishan Mar 14 '13 at 16:33
1

you can use TelePhonyManager.getLineNumber() as other suggested. But I should add one thing. That this is not gurranteed for every operator to get the telephone number by this method. because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. So in some network it may not return the number. So to me the best solution is to ask user to enter telephone number if you want to make your application global.

Moreover it is a duplicate question. See here https://stackoverflow.com/a/6797278/931982.

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112