【问题标题】:Retrieving Line1 number from TelephonyManager in Android从 Android 中的 TelephonyManager 检索 Line1 号码
【发布时间】:2014-09-16 05:10:58
【问题描述】:
TelephonyManager manager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String strMobile1 = manager1.getLine1Number();
这是我获取手机号码的代码,但它不起作用。我也在清单文件中获得了必要的许可
【问题讨论】:
标签:
android
telephonymanager
【解决方案1】:
我们在项目中遇到了同样的问题。结论是它取决于 SIM 卡。
我们发生了什么事:
Galaxy S with AT&T SIM card: can read phone number, Settings shows number
Same Galaxy with an European SIM card: cannot read the number and "unknown" in Settings (cell phone was perfectly functional, just couldn't read the number)
This has been reported in other forums as well.
In the end we decided to ask the user for the phone number. A bit more involved, actually: if( "SIM card present" && "cannot read the cell number") "ask user"; . Otherwise we will keep bugging the user that doesn't a SIM card in the first place.
getLine1Number() only get the number stored on the sim card..a lot of operators don't write there the sim number..on some phones (nokia, blackberry, some olds android, iphone) you can set your own mobile number and the phone will store it on the sim card, at that point getLine1Number() will return the phone number
【解决方案2】:
您使用的方法是 SDK 中唯一可以执行此操作的部分,并且仅适用于将号码存储在 SIM 卡上的设备,只有部分运营商可以这样做。
对于所有其他运营商,您必须要求用户手动输入电话号码,因为该号码根本不会存储在设备上任何您可以检索的地方。
source here