Get my phone number in android

后端 未结 6 910
时光说笑
时光说笑 2020-11-27 15:03

How can I get my phone number in Android? When I use:

TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhon         


        
相关标签:
6条回答
  • 2020-11-27 15:23

    Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.

    editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
    TelephonyManager tMgr;
    tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String mPhoneNumber = tMgr.getLine1Number();
    if (mPhoneNumber != null){
    editTextPhoneNumber.setText(mPhoneNumber);
    }
    
    0 讨论(0)
  • 2020-11-27 15:31

    Method 1:

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

    With below permission

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

    Method 2:

    There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.

    Try below code:

    String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
    Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
            main_data, "mimetype=?",
            new String[]{"vnd.android.cursor.item/phone_v2"},
            "is_primary DESC");
    if (object != null) {
        do {
            if (!((Cursor) (object)).moveToNext())
                break;
            String s1 = ((Cursor) (object)).getString(4);
        } while (true);
        ((Cursor) (object)).close();
    }
    

    You will need to add these two permissions.

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

    Hope this helps, Thanks!

    0 讨论(0)
  • 2020-11-27 15:34

    If the function you called returns null, it means your phone number is not registered in your contact list.

    If instead of the phone number you just need an unique number, you may use the sim card's serial number:

        TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String getSimSerialNumber = telemamanger.getSimSerialNumber();  
    
    0 讨论(0)
  • 2020-11-27 15:37

    From the documentation:

    Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.

    So you have done everything right, but there is no phone number stored.

    If you get null, you could display something to get the user to input the phone number on his/her own.

    0 讨论(0)
  • 2020-11-27 15:37

    As Answered here

    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-27 15:40
    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 讨论(0)
提交回复
热议问题