The exact solution "Get a phone number in Android"

I am trying to get my phone number using this code

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

from

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

But it does not work regularly.

What is the exact β€œGet Android Phone Number” solution?

Thanks x

+4
source share
5 answers

What is the exact β€œGet Android Phone Number” solution?

Enter the username in EditText , possibly with android:inputType="phone" . There is nothing in the Android SDK that would support getting the phone number of the device, since the device may not know its phone number - this is only necessary for the carrier. In addition, there are several scenarios in which the user needs to indicate which of the possible phone numbers they want to use:

  • dual SIM phones
  • People with other phone numbers transferred to this device, or using call forwarding to route calls to these devices.
  • people with SIP numbers or other VOIP phone numbers that they prefer to use with the device
+4
source

Send millions from your phone programmatically to an email address on your server and read the number from the number.

+1
source

TelephonyManager is not the right solution. Because in some cases, the number is not saved on the SIM card. Due to my suggestion, you should use the Shared Preference to store the user number for the first time the application is open, and after that the number will be used whenever you need the application.

+1
source
 public String myNO() { return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getLine1Number(); } 

try it

0
source

Use this code,

  private String getMyPhoneNumber(){ TelephonyManager mTelephonyMgr; mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); return mTelephonyMgr.getLine1Number(); } private String getMy10DigitPhoneNumber(){ String s = getMyPhoneNumber(); return s.substring(2); } 
0
source

Source: https://habr.com/ru/post/1412791/


All Articles