3

I tried the following code to get my own number and get a NULL string. What is the problem?

TelephonyManager mTelephonyMgr;     
 mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);     
 String phoneNumber = mTelephonyMgr.getLine1Number();

Permission

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
Burro
  • 65
  • 1
  • 2
  • 11

2 Answers2

3

Use TelephonyManager.getLine1Number()

But you need to have stored the phone number on the SIM card, and there´s no other way to get your "own number".

Example:

TelephonyManager tm = (TelephonyManager)this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
String myPhoneNumber =  tm.getLine1Number();

and you need this permission into your Manifest.xml

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

See this Question: TelephonyManager.getLine1Number() failing? as describe, you need to evaluate:

if("SIM card present" && "phone number is stored") {
  "get the phone number with TelephonyManager.getLine1Number"
}
Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

Add this to your manifest

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

You need the user's permission to retrieve this data

If it still returns null, that means the data is not available, and you cant use it.

See Get my phone number in android

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145