to get phone number programmatically in Android

こ雲淡風輕ζ 提交于 2019-11-26 14:22:51

问题


I am using the code

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

to get the phone no programatically in android . But this is working fine only for one sim card. If i test this code by inserting other sim card, it is giving null. I am trying to find the solution for this. please help me. I have also included READ_PHONE_STATE permission in Manifest.

I want to uniquely identify the sim card. IS there any other way to do this. please let me know.


回答1:


I think Sim serial Number is unique. You can try this.

TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();

Let me know if there is any issue.




回答2:


This is a known issue. Many SIM across the globe would return null. You should rather use IMEI number however even that can return null.

TelephonyManager.getDeviceId()



回答3:


Look for TelephonyManager.getSubscriberId(), this will give the unique id for each SIM.




回答4:


With API Level greater than or equal to 22 : You can access all the SIM details using SubscritionManager. Use following command to list all Sim numbers(Works well for Dual Sim also):

List<SubscriptionInfo> list = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for(SubscriptionInfo s : list){
   Log.d("SIMMANAGER",s.getNumber());
}

Note : This only works well when the operator stored their mobile number in sim card. Else, this method returns null.



来源:https://stackoverflow.com/questions/5948446/to-get-phone-number-programmatically-in-android

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