3

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)

apaderno
  • 28,547
  • 16
  • 75
  • 90
puppy
  • 43
  • 2
  • 6

2 Answers2

1

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;
       }
Varun Chatterji
  • 5,059
  • 1
  • 23
  • 24
  • thanks for your answer, but I have already tried this kind of method. manager.getSimState() always return 5(SIM_STATE_READY) if the phone is unlocked. It refers to current state, but not the setting option value. – puppy Mar 16 '11 at 07:58
  • Yes, if your code was a service running at startup, then while the code was not entered at phone bootup, you would get these states... If you've already entered the code and are stating your program after that event, you would get SIM_STATE_READY – Varun Chatterji Mar 16 '11 at 08:01
  • yes, it would be an alternative answer if no any better solution, thank you~ – puppy Mar 16 '11 at 08:06
  • Look at this link: http://netmite.com/android/mydroid/frameworks/base/telephony/java/com/android/internal/telephony/SimCard.java /** * Check whether sim pin lock is enabled * This is a sync call which returns the cached pin enabled state * * @return true for sim locked enabled * false for sim locked disabled */ boolean getSimLockEnabled (); – Varun Chatterji Mar 16 '11 at 08:09
  • And if you want to access the internal APIs. The following link has some ideas on how to use reflection to do so: http://stackoverflow.com/questions/2001146/reflection-to-access-advanced-telephony-features – Varun Chatterji Mar 16 '11 at 08:18
  • 1
    I have tried adding ITelephony.aidl to use this method, but it.isSimPinEnabled() always return false even if I turn on the PIN option. In order to test if the project is well-configured, I noticed that it.isDataConnectivityPossible() does return the correct value if I change the phone state(switch to airplain mode) any idea? thx~ (test under htc hero 2.1 update1) – puppy Mar 16 '11 at 15:31
0

As getSimLockEnabled always returns false for me, i had to find another way to do it.

See https://stackoverflow.com/a/12748638/314089 for the answer.

Community
  • 1
  • 1
icyerasor
  • 4,973
  • 1
  • 43
  • 52