Android 如何在真机中获取当前sim卡的手机号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2206596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:57:29  来源:igfitidea点击:

How to get the mobile number of current sim card in real device?

androidphone-number

提问by Narasimha

I have to make an application which shows the contact no of the SIM card that is being used in the cell. For that I need to use TelephonyManagerclass. Can I get details on its usage?

我必须制作一个应用程序,显示单元中正在使用的 SIM 卡的联系方式。为此,我需要使用TelephonyManager类。我可以获得有关其使用的详细信息吗?

回答by Dave Webb

You can use the TelephonyManagerto do this:

您可以使用TelephonyManager这样做:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number = tm.getLine1Number();

The documentation for getLine1Number()says this method will return nullif the number is "unavailable", but it does not say when the number might be unavailable.

对文件getLine1Number()说,此方法将返回null如果该号码是“不可用”,但它并没有说当数可能不可用。

You'll need to give your application permission to make this queryby adding the following to your Manifest:

您需要通过将以下内容添加到您的清单来授予您的应用程序进行此查询权限

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

(You shouldn't use TelephonyManager.getDefault()to get the TelephonyManageras that is a private undocumented API call and may change in future.)

(您不应该使用TelephonyManager.getDefault()获取 ,TelephonyManager因为这是一个私有的未记录的 API 调用,将来可能会更改。)

回答by Amarnath Baitha

Hi Actually this is my same question but I didn't get anything.Now I got mobile number and his email-Id from particular Android real device(Android Mobile).Now a days 90% people using what's App application on Android Mobile.And now I am getting Mobile no and email-ID Through this What's app API.Its very simple to use see this below code.

嗨,实际上这是我的同一个问题,但我什么也没得到。现在我从特定的 Android 真实设备(Android 移动设备)获得了手机号码和他的电子邮件 ID。现在,有 90% 的人在 Android 移动设备上使用什么应用程序。而且现在我通过这个 What's app API 获得了手机号码和电子邮件 ID。使用起来非常简单,请参阅下面的代码。

            AccountManager am = AccountManager.get(this);
            Account[] accounts = am.getAccounts();
      for (Account ac : accounts) 
       {
        acname = ac.name;

        if (acname.startsWith("91")) {
            mobile_no = acname;
        }else if(acname.endsWith("@gmail.com")||acname.endsWith("@yahoo.com")||acname.endsWith("@hotmail.com")){
            email = acname;
        }

        // Take your time to look at all available accounts
        Log.i("Accounts : ", "Accounts : " + acname);
    }

and import this API

并导入这个 API

    import android.accounts.Account;
    import android.accounts.AccountManager;

回答by Yasar Khan

I have to make an application which shows the Contact no of the SIM card that is being used in the cell. For that I need to use Telephony Manager class. Can i get details on its usage?

我必须制作一个应用程序来显示单元中正在使用的 SIM 卡的联系方式。为此,我需要使用电话管理器类。我可以获得有关其使用的详细信息吗?

Yes, You have to use Telephony Manager;If at all you not found the contact no. of user; You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...

是的,您必须使用电话管理器;如果您根本没有找到联系方式。用户;您可以使用相同的 Telephony Manager Class 获取 Sim 卡的 Sim 序列号和 Android 设备的 Imei 号...

Add permission:

添加权限:

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

Import:

进口:

import android.telephony.TelephonyManager;

Use the below code:

使用以下代码:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

         // get IMEI
         imei = tm.getDeviceId();

         // get SimSerialNumber
         simSerialNumber = tm.getSimSerialNumber();

回答by Amit

Well, all could be temporary hacks, but there is no way to get mobile number of a user. It is against ethical policy.

好吧,一切都可能是临时黑客,但没有办法获得用户的手机号码。这是违反道德政策的。

For eg, one of the answers above suggests getting all accounts and extracting from there. And it doesn't work anymore! All of these are hacks only.

例如,上述答案之一建议获取所有帐户并从中提取。它不再起作用了!所有这些都只是黑客。

Only way to get user's mobile number is going through operator. If you have a tie-up with mobile operators like Aitel, Vodafone, etc, you can get user's mobile number in header of request from mobile handset when connected via mobile network internet.

获取用户手机号码的唯一方法是通过运营商。如果您与 Aitel、Vodafone 等移动运营商有合作关系,通过移动网络 Internet 连接时,您可以在手机的请求头中获取用户的手机号码。

Not sure if any manufacturer tie ups to get specific permissions can help - not explored this area, but nothing documented atleast.

不确定是否有任何制造商合作以获得特定权限可以提供帮助 - 没有探索过这个领域,但至少没有任何记录。

回答by ziggestardust

Sometimes you can retreive the phonenumber with a USSD request to your operator. For example I can get my phonenumber by dialing *116# This can probably be done within an app, I guess, ifthe USSD responce somehow could be catched. Offcourse this is not a method I would recommend to use within an app that is to be distributed, the code may even differ between operators.

有时,您可以通过向运营商发送 USSD 请求来检索电话号码。例如,我可以通过拨打 *116# 获取我的电话号码,我想这可能可以在应用程序中完成,如果USSD 的响应可以以某种方式被捕获。当然,这不是我建议在要分发的应用程序中使用的方法,代码甚至可能因运营商而异。

回答by Lukas

As many said:

正如许多人所说:

String phoneNumber = TelephonyManager.getDefault().getLine1Number();

The availability depends strictly on the carrier and the way the number is encoded on the SIM card. If it is hardcoded by the company that makes the SIMs or by the mobile carrier itself. This returns the same as in Settings->about phone.

可用性严格取决于运营商和 SIM 卡上的号码编码方式。如果它是由制造 SIM 的公司或移动运营商本身硬编码的。这与设置->关于手机中的返回相同。