Programmatically obtain the phone number of the Android phone

后端 未结 18 1727
南笙
南笙 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:40

    There is a new Android api that allows the user to select their phonenumber without the need for a permission. Take a look at: https://android-developers.googleblog.com/2017/10/effective-phone-number-verification.html

    // Construct a request for phone numbers and show the picker
    private void requestHint() {
        HintRequest hintRequest = new HintRequest.Builder()
           .setPhoneNumberIdentifierSupported(true)
           .build();
    
        PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(
            apiClient, hintRequest);
        startIntentSenderForResult(intent.getIntentSender(),
            RESOLVE_HINT, null, 0, 0, 0);
    } 
    
    0 讨论(0)
  • A little contribution. In my case, the code launched an error exception. I have needed put an annotation that for the code be run and fix that problem. Here I let this code.

    public static String getLineNumberPhone(Context scenario) {
        TelephonyManager tMgr = (TelephonyManager) scenario.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String mPhoneNumber = tMgr.getLine1Number();
        return mPhoneNumber;
    }
    
    0 讨论(0)
  • 2020-11-21 06:50

    First of all getting users mobile number is against the Ethical policy, earlier it was possible but now as per my research there no solid solution available for this, By using some code it is possible to get mobile number but no guarantee may be it will work only in few device. After lot of research i found only three solution but they are not working in all device.

    There is the following reason why we are not getting.

    1.Android device and new Sim Card not storing mobile number if mobile number is not available in device and in sim then how it is possible to get number, if any old sim card having mobile number then using Telephony manager we can get the number other wise it will return the “null” or “” or “??????”

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
     TelephonyManager tel= (TelephonyManager)this.getSystemService(Context.
                TELEPHONY_SERVICE);
        String PhoneNumber =  tel.getLine1Number();
    

    Note:- I have tested this solution in following device Moto x, Samsung Tab 4, Samsung S4, Nexus 5 and Redmi 2 prime but it doesn’t work every time it return empty string so conclusion is it's useless

    1. This method is working only in Redmi 2 prime, but for this need to add read contact permission in manifest.

    Note:- This is also not the guaranteed and efficient solution, I have tested this solution in many device but it worked only in Redmi 2 prime which is dual sim device it gives me two mobile number first one is correct but the second one is not belong to my second sim it belong to my some old sim card which i am not using.

     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");
        String s1="";
        if (object != null) {
            do {
                if (!((Cursor) (object)).moveToNext())
                    break;
                // This is the phoneNumber
                 s1 =s1+"---"+ ((Cursor) (object)).getString(4);
            } while (true);
            ((Cursor) (object)).close();
        }
    
    1. In my research i have found earlier it was possible to get mobile number using WhatsApp account but now new Whatsapp version doesn’t storing user's mobile number.

    Conclusion:- Android doesn’t have any guaranteed solution to get user's mobile number programmatically.

    Suggestion:- 1. If you want to verify user’s mobile number then ask to user to provide his number, using otp you can can verify that.

    1. If you want to identify the user’s device, for this you can easily get device IMEI number.
    0 讨论(0)
  • 2020-11-21 06:51

    while working on a security app which needed to get the phone number of who so ever my phone might get into their hands, I had to do this; 1. receive Boot completed and then try getting Line1_Number from telephonyManager which returns a string result. 2. compare the String result with my own phone number and if they don't match or string returns null then, 3. secretly send an SMS containing the string result plus a special sign to my office number. 4. if message sending fails, start a service and keep trying after each hour until sent SMS pending intent returns successful. With this steps I could get the number of the person using my lost phone. it doesn't matter if the person is charged.

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

    Add this dependency: implementation 'com.google.android.gms:play-services-auth:18.0.0'

    To fetch phone number list use this:

    val hintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()
    
    val intent = Credentials.getClient(context).getHintPickerIntent(hintRequest)
    
    startIntentSenderForResult(
        intent.intentSender,
        PHONE_NUMBER_FETCH_REQUEST_CODE,
        null,
        0,
        0,
        0,
        null
    )
    

    After tap on play services dialog:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent? { 
        super.onActivityResult(requestCode, resultCode, data)
    
        if (requestCode == PHONE_NUMBER_FETCH_REQUEST_CODE) {
            data?.getParcelableExtra<Credential>(Credential.EXTRA_KEY)?.id?.let { 
                useFetchedPhoneNumber(it)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:54

    Sometimes, below code returns null or blank string.

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

    With below permission

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

    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;
            // This is the phoneNumber
            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)
提交回复
热议问题