在Android中从TelephonyManager检索Line1号

在Android中从TelephonyManager检索Line1号,android,telephonymanager,Android,Telephonymanager,这是我获取手机号码的代码,但它不起作用。我还在清单文件中获得了必要的许可,我们在项目中遇到了相同的问题。结论是,这取决于SIM卡 发生在我们身上的事: TelephonyManager manager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String strMobile1 = manager1.getLine1Number(); 您使用的方法是SDK中唯一一个可以实现此目的的部分,并且仅

这是我获取手机号码的代码,但它不起作用。我还在清单文件中获得了必要的许可,我们在项目中遇到了相同的问题。结论是,这取决于SIM卡

发生在我们身上的事:

TelephonyManager manager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String strMobile1 = manager1.getLine1Number();  

您使用的方法是SDK中唯一一个可以实现此目的的部分,并且仅适用于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