以编程方式获取Android手机的电话号码
如何以编程方式获取运行 Android 应用程序的设备的电话号码?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何以编程方式获取运行 Android 应用程序的设备的电话号码?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(24)
代码:
所需权限:
注意事项:
根据高度评价的评论,有一些注意事项需要注意。这可以返回
null
或""
甚至"???????"
,并且它可以返回一个过时的电话号码,即不再有效。如果您想要唯一标识设备的内容,则应该使用getDeviceId()
。Code:
Required Permission:
Caveats:
According to the highly upvoted comments, there are a few caveats to be aware of. This can return
null
or""
or even"???????"
, and it can return a stale phone number that is no longer valid. If you want something that uniquely identifies the device, you should usegetDeviceId()
instead.由于电话号码并未实际存储在所有 SIM 卡上或从网络广播到电话,因此无法保证解决此问题。在一些需要实际地址验证的国家尤其如此,号码分配仅在事后进行。电话号码分配发生在网络上 - 并且可以在不更改 SIM 卡或设备的情况下进行更改(例如,这就是支持移植的方式)。
我知道这很痛苦,但最有可能最好的解决方案就是要求用户输入他/她的电话号码一次并存储它。
There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).
I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it.
更新:此答案不再可用,因为 Whatsapp 已停止将电话号码公开为帐户名,请忽略此答案。
如果您无法通过电话服务获得它,实际上您可能需要考虑另一种解决方案。
从今天开始,您可以依赖另一个大型应用程序 Whatsapp,使用
AccountManager
。数以百万计的设备安装了此应用程序,如果您无法通过TelephonyManager
获取电话号码,您可以尝试一下。权限:
代码:
检查 WhatsApp 帐户的
actype
当然,如果用户没有安装 WhatsApp,您可能无法获得它,但无论如何值得尝试。
请记住,您应该始终要求用户确认。
Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.
There is actually an alternative solution you might want to consider, if you can't get it through telephony service.
As of today, you can rely on another big application Whatsapp, using
AccountManager
. Millions of devices have this application installed and if you can't get the phone number viaTelephonyManager
, you may give this a shot.Permission:
Code:
Check
actype
for WhatsApp accountOf course you may not get it if user did not install WhatsApp, but its worth to try anyway.
And remember you should always ask user for confirmation.
这就是您在未经许可和黑客攻击的情况下通过 Play Services API 请求电话号码的方式。 来源和完整示例。
在您的 build.gradle 中(需要 10.2.x 及更高版本):
在您的 Activity 中(代码已简化):
这将生成一个如下所示的对话框:
So that's how you request a phone number through the Play Services API without the permission and hacks. Source and Full example.
In your build.gradle (version 10.2.x and higher required):
In your activity (the code is simplified):
This will generate a dialog like this:
正如我的 早期答案中发布的那样,
使用以下代码:
在 AndroidManifest.xml 中,授予以下权限:
但请记住,此代码确实并不总是有效,因为手机号码取决于 SIM 卡和网络运营商/手机运营商。
另外,请尝试检查电话-->设置-->关于-->电话号码,如果您能够在此处查看号码,则从上述代码中获取电话号码的概率较高。如果您无法在设置中查看电话号码,那么您将无法通过此代码获得联系!
建议的解决方法:
在应用程序首次启动期间将上述 4 个步骤作为一次性活动执行。稍后,每当需要电话号码时,请使用共享首选项中可用的值。
As posted in my earlier answer
Use below code :
In AndroidManifest.xml, give the following permission:
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.
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!
Suggested Workaround:
Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.
有一个新的 Android API,允许用户选择他们的电话号码而无需许可。看看:
https://android-developers.googleblog.com/2017 /10/ effective-phone-number-verification.html
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
代码取自 http://www.androidsnippets.com/get-my-phone-number
Code taken from http://www.androidsnippets.com/get-my-phone-number
只是想在上面的答案中添加一些解释。这也将为其他人节省时间。
就我而言,此方法没有返回任何手机号码,而是返回了一个空字符串。这是由于我将我的号码转移到新SIM卡上的情况。因此,如果我进入“设置”>“关于电话”>“状态”>“我的电话号码”,它会显示“未知”。
Just want to add a bit here to above explanations in the above answers. Which will save time for others as well.
In my case this method didn't returned any mobile number, an empty string was returned. It was due to the case that I had ported my number on the new sim. So if I go into the Settings>About Phone>Status>My Phone Number it shows me "Unknown".
有时,下面的代码会返回
null
或空白字符串。获得以下许可
还有另一种方法可以获取您的电话号码,我尚未在多个设备上测试过此方法,但上述代码并非每次都有效。
尝试下面的代码:
您将需要添加这两个权限。
希望这有帮助,
谢谢!
Sometimes, below code returns
null
or blank string.With below permission
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:
You will need to add these two permissions.
Hope this helps,
Thanks!
首先,获取用户手机号码是违反道德政策的,以前这是可能的,但现在根据我的研究,没有可靠的解决方案可用,通过使用一些代码,可以获取手机号码,但不能保证它会起作用仅在少数设备中。经过大量研究,我只发现了三种解决方案,但它们并不适用于所有设备。
我们没有得到的原因如下。
1.Android设备和新SIM卡不存储手机号码,如果设备和SIM卡中没有手机号码,那么如何获取号码,如果任何旧SIM卡有手机号码,那么使用电话管理器我们可以获取其他号码明智的它会返回“null”或“”或“??????”
读取清单中的联系人权限。
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 “??????”
read contact permission in manifest.
TelephonyManager
不是正确的解决方案,因为在某些情况下,号码不会存储在 SIM 中。我建议您应该在应用程序首次打开时使用共享首选项来存储用户的电话号码,以便在需要时使用该号码。TelephonyManager
is not the right solution, because in some cases the number is not stored in the SIM. I suggest that you should use the shared preference to store the user's phone number for the first time the application is open and the number will used whenever you need.以下是我找到的解决方案的组合(示例项目此处< /a>,如果您还想检查自动填充):
manifest
build.gradle
MainActivity.kt
Here's a combination of the solutions I've found (sample project here, if you want to also check auto-fill):
manifest
build.gradle
MainActivity.kt
这是一个更简化的答案:
This is a more simplified answer:
添加此依赖项:
implementation 'com.google.android.gms:play-services-auth:18.0.0'
要获取电话号码列表,请使用以下命令:
点击播放服务对话框后:
Add this dependency:
implementation 'com.google.android.gms:play-services-auth:18.0.0'
To fetch phone number list use this:
After tap on play services dialog:
#SaveBangladesh
注意:为了避免 PlayConsole 中的政策违规:在 AppIntro 屏幕中,您需要有一个 PrivacyPolicy 屏幕。在隐私政策部分,您需要解释为什么您从用户那里获取电话号码。
#SaveBangladesh
Note: To Avoid Policy Violation in PlayConsole: In AppIntro Screen, you need to have a PrivacyPolicy Screen. And in PrivacyPolicy section you need to explain why you get phone numbers from users.
一点贡献。就我而言,代码引发了错误异常。我需要添加一个注释来运行代码并解决该问题。这里我让这段代码。
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.
我注意到几个答案发布了相同的内容。首先,发生了变化,
onActivityResult
已被弃用,随后HintRequest
也已被弃用。这是 2023 年未弃用的解决方案。注意:虽然许多人认为这可以让您检索用户的手机号码。通常情况并非如此。 Google Play 服务缓存了很少的电话号码,有时对话框会显示不属于用户的电话号码。
文档
I noticed several answers posting the same thing. First of all things changed,
onActivityResult
is deprecated followed byHintRequest
which also got deprecated. Here is the non-deprecated solution from 2023.Note: While many of you think this allows you to retrieve user's mobile phone number. That is usually not the case. Google Play Services has cached few phone numbers and sometimes the dialog shows phone numbers in which none belongs to user.
Documentation
尽管可以拥有多个语音信箱帐户,但当使用您自己的号码拨打电话时,运营商会将您转至语音信箱。因此,
TelephonyManager.getVoiceMailNumber()
或TelephonyManager.getCompleteVoiceMailNumber()
,具体取决于您需要的风格。希望这有帮助。
Although it's possible to have multiple voicemail accounts, when calling from your own number, carriers route you to voicemail. So,
TelephonyManager.getVoiceMailNumber()
orTelephonyManager.getCompleteVoiceMailNumber()
, depending on the flavor you need.Hope this helps.
不建议使用
TelephonyManager
,因为它要求应用在运行时需要READ_PHONE_STATE
权限。应该使用 Google 的 Play 服务进行身份验证,并且它将能够允许用户选择要使用的电话号码,并处理多个 SIM 卡,而不是我们试图猜测哪一张是主 SIM 卡。
Wouldn't be recommending to use
TelephonyManager
as it requires the app to requireREAD_PHONE_STATE
permission during runtime.Should be using Google's Play Service for Authentication, and it will able to allow User to select which phoneNumber to use, and handles multiple SIM cards, rather than us trying to guess which one is the primary SIM Card.
对于 Android 版本 >= LOLLIPOP_MR1 :
添加权限:
并称之为:
For android version >= LOLLIPOP_MR1 :
Add permission :
And call this :
如果我从
voiceMailNumer
获取号码,那么它工作正常 -If I'm getting number from
voiceMailNumer
then it is working good -首先像这样初始化您的登录 Intent
要打开 google play 意图并显示与 google 帐户关联的电话号码,请使用此
注意:
Firstly Initalize your sign in Intent like this
To open google play intent and show phone number associated with google account use this
Note:
这适用于 SDK 33
AndroidManifest.xml
This will work on SDK 33
AndroidManifest.xml
在开发一个安全应用程序时,该应用程序需要获取谁的电话号码,以便我的手机可能落入他们手中,我必须这样做;
1. 接收启动完成,然后尝试从 telephonyManager 获取 Line1_Number,它返回字符串结果。
2.将字符串结果与我自己的电话号码进行比较,如果不匹配或字符串返回null,
3. 秘密发送一条包含字符串结果加上特殊符号的短信到我的办公室号码。
4. 如果消息发送失败,则启动一个服务并每隔一小时继续尝试,直到发送的短信挂起意图返回成功。
通过这些步骤,我可以获得使用我丢失的手机的人的号码。
该人是否被指控并不重要。
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.