Take the 2-minute tour ×

I am doing one application using proximity sensor in android. when sensor changed it should lock the phone and when phone is locked using same sensor it should unlock a phone. To lock a phone am using double tap mechanisam. for lock using only a single tap. my code is like below:

 public void onSensorChanged(SensorEvent event) {
 // TODO Auto-generated method stub

         if(curTime2 - curTime1 < 1000) 
        if(Tap==2 ) //&& (curTime2 - curTime1)==100000)
          mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);                 
            mDeviceAdminSample = new ComponentName(Controller.this,
          active = mDPM.isAdminActive(mDeviceAdminSample);
            flagLock = true;

         // unlock

     if(flagLock == false){ 
         mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
     mLock = mKeyGuardManager.newKeyguardLock("activity_classname");

The unlock code is working on first tap only. I need to it should execute after the phone is locked but it is not working. How to do this? Thx in advance

share|improve this question

1 Answer 1

When phone is locked your application gets into standby mode so code writen do no work. You need keep your app open to make it work when phone is locked.for this you need to take granted permission from user and change phone settings before executing this code.

share|improve this answer
You can use a services to do actions when your app activities are not visible. –  resus Nov 13 at 15:03

Your Answer


