best way to retrieve phone number?

两盒软妹~` 提交于 2019-12-08 00:49:44

问题


i need to be able to get the SIM's phone number, currently i have been using:

telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
tmPhone = telephonyManager.getLine1Number();

but this method doesnt always return a result/the correct result.

is there anyway of getting the correct phone number 100% of the time, without asking the user to manually enter it?


回答1:


Use This.

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
String phone = tm.getLine1Number();

but it's not always reliable on for example non phone device. You will also need to add permission "android.permission.READ_PHONE_STATE".



来源:https://stackoverflow.com/questions/7268452/best-way-to-retrieve-phone-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!