问题
I try to get phone number using getLine1Number()
in android,but it returns null . Also sometimes I need to use another sim and get its number.
How to always get phone number ?
回答1:
If you are referring to get the phone number from android you can try this !!
TelephonyManager phoneManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
Add the permission in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Workaround:
- In your code if you really need users phone number and the above code
returns
null
- If you get
null
, you could display something to get the user to input the phone number on his/her own.
回答2:
You can get phone number with below code :
TelephonyManager tele = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = tele.getLine1Number();
And add following permission to manifest file :
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
回答3:
On GSM network, basically you can't! The phone number is only know by the Network and not by the phone or the SIM.
It's the operators network who associate the IMEI SIM number with a valid cell phone. And that's the reason why number portability can be achieved: you can have a different IMEI with the same phone number, or you can have an IMEI with a phone number that changes later on.
You can send an SMS to a specific service if you really want to know it, or ask directly to the end user.
回答4:
Using TelephonyManager
you can achive this.
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Specify this line in the manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
回答5:
As posted in my earlier answer
Use below code :
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, give the following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
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:
- Get the user's phone number as manual input from the user.
- Send a code to the user's mobile number via SMS.
- Ask user to enter the code to confirm the phone number.
- Save the number in sharedpreference.
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.
回答6:
TelephonyManager tele = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = tele.getLine1Number();
回答7:
The number you get via getLine1Number()
API is stored in SIM/USIM card. So the result of this function depend on the implement of SIM/USIM card.
来源:https://stackoverflow.com/questions/26376050/how-to-get-phone-number-android-phone