Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect if PIN code is required to unlock sim?

There is a "SIM card lock" option in android "setting/Location & security settings" page.

It's necessary to input a PIN code after booting if the option is set.

Is there any programmatic method to detect if PIN is required ? (not current sim state but the setting option value ex: true/false)

like image 808
puppy Avatar asked Feb 02 '26 07:02

puppy


1 Answers

You can use the following class: TelephonyManager http://developer.android.com/reference/android/telephony/TelephonyManager.html

You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE)

TelephonyManager manager = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getSimState();
if(state == TelephonyManager.SIM_STATE_PIN_REQUIRED || state == TelephonyManager.SIM_STATE_PUK_REQUIRED)
{
         //PIN/PUK is required
}

Following the comments, this is the final version:

TelephonyManager tm = 
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
       Class clazz = Class.forName(tm.getClass().getName());
       Method m = clazz.getDeclaredMethod("getITelephony");
       m.setAccessible(true);
       ITelephony it = (ITelephony) m.invoke(tm);
       if(it.isSimPinEnabled())
       {
                //This should work;
       }
like image 167
Varun Chatterji Avatar answered Feb 03 '26 19:02

Varun Chatterji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!