Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

android - TelephonyManager.getLine1Number() failing?

I want to get phone number of android device. I use this code:

TelephonyManager tm = (TelephonyManager)this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
String phoneNumber=  tm.getLine1Number();

On my phone (HTC Wildfire) I can not get phone number - all I get is empty string. If I run code on emulatur, phone number is normally showed.

If I go to Settings=>About phone=>Phone identity->Phone number, phone number is "Unknown".

Where is the problem? Is something wrong with my SIM?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We had the same problem in our project. The conclusion was that it depends on the SIM card.

What happened to us:

  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...