Programmatically obtain the phone number of the Android phone

后端 未结 18 1724
南笙
南笙 2020-11-21 06:17

How can I programmatically get the phone number of the device that is running my android app?

相关标签:
18条回答
  • 2020-11-21 06:31

    As posted in my earlier answer

    Use below code :

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

    In AndroidManifest.xml, give the following permission:

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

    But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.

    Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!

    Suggested Workaround:

    1. Get the user's phone number as manual input from the user.
    2. Send a code to the user's mobile number via SMS.
    3. Ask user to enter the code to confirm the phone number.
    4. Save the number in sharedpreference.

    Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.

    0 讨论(0)
  • 2020-11-21 06:32

    There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).

    I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it.

    0 讨论(0)
  • 2020-11-21 06:32

    Just want to add a bit here to above explanations in the above answers. Which will save time for others as well.

    In my case this method didn't returned any mobile number, an empty string was returned. It was due to the case that I had ported my number on the new sim. So if I go into the Settings>About Phone>Status>My Phone Number it shows me "Unknown".

    0 讨论(0)
  • 2020-11-21 06:36

    Although it's possible to have multiple voicemail accounts, when calling from your own number, carriers route you to voicemail. So, TelephonyManager.getVoiceMailNumber() or TelephonyManager.getCompleteVoiceMailNumber(), depending on the flavor you need.

    Hope this helps.

    0 讨论(0)
  • 2020-11-21 06:38

    Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.

    There is actually an alternative solution you might want to consider, if you can't get it through telephony service.

    As of today, you can rely on another big application Whatsapp, using AccountManager. Millions of devices have this application installed and if you can't get the phone number via TelephonyManager, you may give this a shot.

    Permission:

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

    Code:

    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccounts();
    
    for (Account ac : accounts) {
        String acname = ac.name;
        String actype = ac.type;
        // Take your time to look at all available accounts
        System.out.println("Accounts : " + acname + ", " + actype);
    }
    

    Check actype for WhatsApp account

    if(actype.equals("com.whatsapp")){
        String phoneNumber = ac.name;
    }
    

    Of course you may not get it if user did not install WhatsApp, but its worth to try anyway. And remember you should always ask user for confirmation.

    0 讨论(0)
  • 2020-11-21 06:39
    private String getMyPhoneNumber(){
        TelephonyManager mTelephonyMgr;
        mTelephonyMgr = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE); 
        return mTelephonyMgr.getLine1Number();
    }
    
    private String getMy10DigitPhoneNumber(){
        String s = getMyPhoneNumber();
        return s != null && s.length() > 2 ? s.substring(2) : null;
    }
    

    Code taken from http://www.androidsnippets.com/get-my-phone-number

    0 讨论(0)
提交回复
热议问题