如何在真实设备中获取当前SIM卡的手机号码? [英] How to get the mobile number of current sim card in real device?
问题描述
我必须创建一个显示该单元中正在使用的SIM卡的联系人号码的应用程序.为此,我需要使用TelephonyManager
类.我可以获取有关其用法的详细信息吗?
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 TelephonyManager
class. Can I get details on its usage?
推荐答案
您可以使用 TelephonyManager
为此:
You can use the TelephonyManager
to do this:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
getLine1Number()
的文档表示如果数字不可用",则此方法将返回null
,但没有说明数字何时不可用.
The documentation for getLine1Number()
says this method will return null
if the number is "unavailable", but it does not say when the number might be unavailable.
您需要授予您的应用程序通过在清单中添加以下内容来进行此查询:
You'll need to give your application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(您不应该使用TelephonyManager.getDefault()
作为
(You shouldn't use TelephonyManager.getDefault()
to get the TelephonyManager
as that is a private undocumented API call and may change in future.)
这篇关于如何在真实设备中获取当前SIM卡的手机号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!