检索电话号码,最好的方法?最好的、电话号码、方法

2023-09-06 18:22:43 作者:別哭勒、怹吥荟吢庝

我需要能够获得SIM卡的电话号码,目前我一直在用:

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.

反正是有得到正确的电话号码的100%的时间,而不要求用户手动输入?

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

推荐答案

使用此。

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

但它不是如非手机设备总是可靠的。您还需要添加权限android.permission.READ_PHONE_STATE。

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