How can I get the ICCID number of the phone?
I looked in the TelephonyManager class, but didn't found there a way to get the ICCID number, there is only a method which tells if the ICC card is present.
How can I get the ICCID number of the phone?
I looked in the TelephonyManager class, but didn't found there a way to get the ICCID number, there is only a method which tells if the ICC card is present.
I believe getSimSerialNumber()
will get iccid.
UPDATE for Android 5.1 (credit Erick M Sprengel)
"Multiple SIM Card Support" was added for Android 5.1 (API 22).
Instead of using getSimSerialNumber
, you can use SubscriptionManager
.
SubscriptionManager sm = SubscriptionManager.from(mContext);
// it returns a list with a SubscriptionInfo instance for each simcard
// there is other methods to retrieve SubscriptionInfos (see [2])
List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
// getting first SubscriptionInfo
SubscriptionInfo si = sis.get(0);
// getting iccId
String iccId = si.getIccId();
You will need the permission: (credit Damian)
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
It's best not to use this, as this is one of the identifiers that recently got restricted, just as was done recently on Android 10.
On Android 11 (API 30), even if you don't target it, you will most likely get just an empty string.
The recommendation of what to use is getSubscriptionId instead (requires READ_PHONE_STATE permission).
The reason:
Returns the ICC ID. Starting with API level 30, returns the ICC ID if the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission, has carrier privileges (see TelephonyManager#hasCarrierPrivileges), or is a device owner or profile owner that has been granted the READ_PHONE_STATE permission. The profile owner is an app that owns a managed profile on the device; for more details see Work profiles. Profile owner access is deprecated and will be removed in a future release.
so it returns "the ICC ID, or an empty string if one of these requirements is not met" . For third party apps, this means you will probably always get an empty string.
If you don't meet these (and you usually don't), you can use it.
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
And then for each of them, you can use subscriptionInfo.getIccId()
.
@Jakar, thank you very much! Here's the same code in C# using Xamarin / Mono.Android
Requires the permission set in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Simple C#
using System.Collections.Generic;
using Android.Telephony;
// ...
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Requires, android.permission.READ_PHONE_STATE
SubscriptionManager sm = Android.Telephony.SubscriptionManager.From(Android.App.Application.Context);
IList<SubscriptionInfo> sis = sm.ActiveSubscriptionInfoList;
SubscriptionInfo si = sis[0];
string carrier = si.CarrierName;
string iccId = si.IccId;
string phoneNum = si.Number;
System.Diagnostics.Debug.WriteLine("Carrier: " + carrier);
System.Diagnostics.Debug.WriteLine("SIM Card IccId: " + iccId);
System.Diagnostics.Debug.WriteLine("Phone Number: " + phoneNum);
// Bonus, Get the IMEI Id (aka, DeviceId)
TelephonyManager tm = (TelephonyManager)GetSystemService(Android.Content.Context.TelephonyService);
string deviceId = tm.DeviceId;
System.Diagnostics.Debug.WriteLine("Device Id: " + deviceId);
}
Both SubscriptionManager and SubscriptionInfo are found under the Android.Telephony namespace
Truth be told this example was needed to activate my phone on the fly and not having the SIM card tool handy to pull it out. And, my eyes aren't what they use to be. View it on DroidTelephonySimInfo.cs on gist.github.com