to get phone number programmatically in Android
Asked Answered
P

4

12

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.

Paralysis answered 10/5, 2011 at 10:6 Comment(1)
can u provide your solution here for getting the phone number programatically.plz can u help.Measly
P
8

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.

Peen answered 21/6, 2011 at 5:35 Comment(3)
Hi, I only got the solution around a month ago. Any way thanks for ur interestParalysis
Hi Prabhu, Could you mention the solution you have got for your issue?Peddle
What about multi sim mobile?Heavily
O
1

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()
Overwork answered 10/5, 2011 at 10:26 Comment(1)
Hey , I know this is a known issue. But I need to uniquely identify the sim card.Paralysis
F
1

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

Formication answered 10/5, 2011 at 10:58 Comment(3)
Hey, TelephonyManager.getSubscriberId() gives same id for two different sims of same network. I am using serial number of sim to uniquely identify simParalysis
No, getSubscriberId return you unique IMSI value for GSM phone, even if you have two different sims of same network. Check this developer.android.com/reference/android/telephony/…Formication
On some phones or networks only the first 6 digits of the 15-digit subscriber id is returned.Boilermaker
C
1

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.

Connolly answered 24/12, 2018 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.