问题
I am using following android code to get mobile number and it's working on android emulators only:
TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String number = tm.getLine1Number();
But when I tested it in real device (Samsung Galaxy Chat B5330
) it gives nothing (Empty String)
Please help with some code snippet.
回答1:
For GSM, the phone number is on the SIM card, and some carriers just don't put it on the card, and then the phone does not know what it is, but in this case you should get an empry string rather than a null
if the carrier stores the number on your SIM, go to Settings -> About Phone -> Status -> My phone Number
. If it displays unknown
there, then your number is not stored on the SIM.
get IMEI use:
TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei_no = tm.getDeviceId();
回答2:
Add permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
See this
回答3:
You have to use Telephony Manager;If at all you not found the contact no. of user; You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...
Add permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Import:
import android.telephony.TelephonyManager;
Use the below code:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
imei = tm.getDeviceId();
// get SimSerialNumber
simSerialNumber = tm.getSimSerialNumber();
来源:https://stackoverflow.com/questions/16333816/get-mobile-number-on-gsm-mobile-using-android-code