Reputation: 51
in which cases this method returns a null reference? Can only depend on the sim card? Doesn't exist, in these cases, an alternative to retrieve an identification number of the latter?
Thanks to all!
Upvotes: 5
Views: 12046
Reputation: 218
getSimSerialNumber() needs privileged permissions from Android 10 onwards, and third party apps can't register this permission.
See : https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids
A possible solution is to use the TELEPHONY_SUBSCRIPTION_SERVICE from Android 5.1, to retrieve the sim serial number. Steps below:
Retrieve the sim details from Subscription Object.
if ( isPermissionGranted(READ_PHONE_STATE) ) {
String simSerialNo="";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManager subsManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subsList = subsManager.getActiveSubscriptionInfoList();
if (subsList!=null) {
for (SubscriptionInfo subsInfo : subsList) {
if (subsInfo != null) {
simSerialNo = subsInfo.getIccId());
}
}
}
} else {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
simSerialNo = tMgr.getSimSerialNumber();
}
}
Check if this helps
Upvotes: 3
Reputation: 2250
try the below code..i will help full for u..
TelehponyManager manager = (TelehponyManager)getSystemService(TELEPHONY_SERVICE);
String imei = manager.getDeviceId();
String imsi = manager.getSubscriberId();
Upvotes: 0
Reputation: 2037
Please make sure whether you have added the following permission in the Android Manifest, if not please add this statement add try again.
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Note: Add this permission tag outside the application tag..
Sample Snippet:
.....
.....
.....
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android......"/>
..........
</manifest>
All the Best
Upvotes: 2