4

I have implemented below given to unlock my app (this code works for only systems apps so I have done my app as system app )

TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getSimState();

if(state == TelephonyManager.SIM_STATE_PIN_REQUIRED || state == TelephonyManager.SIM_STATE_PUK_REQUIRED)
{
   try {

      @SuppressWarnings("rawtypes")
      Class clazz = Class.forName(manager.getClass().getName());

       Method m = clazz.getDeclaredMethod("getITelephony");
       m.setAccessible(true);
       ITelephony it = (ITelephony) m.invoke(manager);
       if (it.supplyPin(simPin)) {
           Toast.makeText(context,"SIM UnLocked",Toast.LENGTH_LONG).show();
       } else {
           Toast.makeText(context,"SIM UNLOCK FAILED",Toast.LENGTH_LONG).show();
       }

    } catch (Exception e) {
    // 
        e.printStackTrace();
    }

}else{
    Toast.makeText(context,"SIM is not Locked",Toast.LENGTH_LONG).show();
}

It works fine for me, but now I need to implement setting or resetting SIM PIN programmatically, let me know if it is possible or not. if possible than how can I implement that?

Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
  • 1
    AFAIK you can execute USSD codes for that. Take a look at http://stackoverflow.com/questions/7225100/how-to-run-ussd-commands-on-android and find the USSD to set/change the pin. I think it can work – Pedro Oliveira Sep 11 '14 at 09:47
  • thanks @PedroOliveira for your responce. but how I can send request for PIN change? – Naveed Ali Sep 11 '14 at 09:52
  • 1
    You can get more info here http://berlin.ccc.de/~tobias/mmi-ussd-ss-codes-explained.html. check the "SIM control codes" part – Pedro Oliveira Sep 11 '14 at 09:59
  • but I think pin is saved at SIM (Locally). I dont think when change pin code we send request to network, but I think it is saved to SIM locally without any network request. if I am wrong let me know – Naveed Ali Sep 11 '14 at 10:05
  • 2
    You're right. But this code is executed locally. You don't have to press Call to execute this code. However, after some search, I don't think you can use an intent to execute this code. But it's worth the try. Try executing the code on your phone numpad and then try it using an intent – Pedro Oliveira Sep 11 '14 at 10:09
  • @PedroOliveira, it worked for me but I cant confirm programmatically wether PIN is set or Not. I have tried with onActivityResult() whether PIN changed or nor, the resultCode is 0. – Naveed Ali Sep 18 '14 at 07:24

1 Answers1

3
String ussdCode = "**04*"+oldPin+"*"+newPin+"*"+newPin+"#";
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));
Muzammal Hussain
  • 377
  • 4
  • 14