如何以编程方式获取设备的电话号码[重复]

时间:2022-05-07 22:55:42

This question already has an answer here:

这个问题在这里已有答案:

I am trying to get phone no using following method:

我正在尝试使用以下方法获取电话:

private String getMyPhoneNumber() {
    TelephonyManager mTelephonyMgr;

    mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
    //String imsi = mTelephonyMgr.getSubscriberId();
    String phnNo = mTelephonyMgr.getLine1Number();
    if (phnNo == null) {
        phnNo = getNoFromWatsApp();
    }
    return phnNo;
}

private String getNoFromWatsApp(){
    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccounts();
    String phoneNumber = "";

    for (Account ac : accounts) {
        String acname = ac.name;
        String actype = ac.type;
        // Take your time to look at all available accounts
        if(actype.equals("com.whatsapp")) {
            phoneNumber = ac.name;
        }
    }
    return phoneNumber;
}

But every time I get an empty phone number. I even tried to get phone number from WhatsApp but it's returning "WhatsApp" instead of phone number. Is there any other way to solve this problem?

但每次我得到一个空电话号码。我甚至试图从WhatsApp获得电话号码,但它返回“WhatsApp”而不是电话号码。有没有其他方法可以解决这个问题?

1 个解决方案

#1


25  

Use below code :

使用以下代码:

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

In AndroidManifest.xml, give the following permission:

在AndroidManifest.xml中,授予以下权限:

 <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.

但请记住,此代码并不总是有效,因为手机号码取决于SIM卡和网络运营商/手机运营商。

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!

此外,尝试检查电话 - >设置 - >关于 - >电话身份,如果您能够在那里查看号码,从上面的代码获取电话号码的概率更高。如果您无法在设置中查看电话号码,那么您将无法通过此代码获取!

#1


25  

Use below code :

使用以下代码:

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

In AndroidManifest.xml, give the following permission:

在AndroidManifest.xml中,授予以下权限:

 <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.

但请记住,此代码并不总是有效,因为手机号码取决于SIM卡和网络运营商/手机运营商。

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!

此外,尝试检查电话 - >设置 - >关于 - >电话身份,如果您能够在那里查看号码,从上面的代码获取电话号码的概率更高。如果您无法在设置中查看电话号码,那么您将无法通过此代码获取!