简体   繁体   中英

getting phone number from android device

Yes I am using the correct permission:

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

And here is my code:

private String getDevicePhoneNumber() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager)
        getSystemService(Context.TELEPHONY_SERVICE);
    return mTelephonyMgr.getLine1Number();
}

The phone is returning lambda to me: the empty string.

Does anyone know how to get this right? I need this to work for android Froyo ( 2.2 ) and up

It's a problem indeed. There is no one solution to that will always work. It is not stored on all SIM cards nor is it accessible always from the carrier network.

Physical address verification might be even require in some countries.

The assignment of a phone number assignment happens on the network - and can be changed without changing the SIM card or device (eg this is how porting is supported to work when moving from one carrier to another..).

you best bet is to ask the user, as strange as that might sound..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM