Get my phone number in android

后端 未结 6 941
时光说笑
时光说笑 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:31

    Method 1:

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

    With below permission

    
    

    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.

    
    
    

    Hope this helps, Thanks!

提交回复
热议问题