Usind Device administrator in android

xeroxy

New Member
I am using device administrator API to lock a screen and mLock.disableKeyguard();to unlock a screen using proximity sensor. My application is working fine for the first time. i.e after executing if i made action it will lock and unlock screen when phone is in locked state.but if I did same action for second time it is not working(not locking). my code is like below:\[code\] if(lockFlag == 0){ mySensorManager.registerListener(proximitySensorEventListener, myProximitySensor, SensorManager.SENSOR_DELAY_NORMAL); mySensorManager.unregisterListener(unLockProximitySensorEventListener); lockFlag = 1; }else if(lockFlag == 1){ mySensorManager.registerListener(unLockProximitySensorEventListener, myProximitySensor,SensorManager.SENSOR_DELAY_FASTEST); mySensorManager.unregisterListener(proximitySensorEventListener); lockFlag = 0; } \[/code\]In the above code i am registering sensors for lock and unlock. below code is for lock screen.\[code\]SensorEventListener proximitySensorEventListener = new SensorEventListener(){ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){ count++ ; long curTime1 = 0, curTime2 = 0 ; if(count%2 ==0) { curTime1 = System.currentTimeMillis(); } else { curTime2 = System.currentTimeMillis(); //System.out.println("Near time "+curTime2); }// if(curTime1 < curTime2) if(curTime2 - curTime1 < 1000) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub if(Tap<=1) { Tap = 0; } } }, 1000); Tap++; if(Tap==2 ) { System.out.println("phone locked +++++++"); mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); mDeviceAdminSample = new ComponentName(Controller.this, LockScreenActivity.class); active = mDPM.isAdminActive(mDeviceAdminSample); System.out.println("valuve of active"+active); System.out.println("***inside if"); mDPM.lockNow(); System.out.println("***inside active after if"); // Tap=0; } } } } //end of sensor changed }; \[/code\]to unlock a screen i am using code like following.\[code\]SensorEventListener unLockProximitySensorEventListener = new SensorEventListener(){ @Override public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){ System.out.println("inside second proximity++++"); count = 0; count1++ ; long curTime1 = 0, curTime2 = 0 ; if(count1%2 ==0) { curTime1 = System.currentTimeMillis(); //System.out.println("Far time "+curTime1); } else { curTime2 = System.currentTimeMillis(); //System.out.println("Near time "+curTime2); }// if(curTime1 < curTime2) if(curTime2 - curTime1 < 1000) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub if(TapUnlock<=1) { TapUnlock = 0; } } }, 1000); TapUnlock++; if(TapUnlock == 2) { //Tap = 0; mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); mDeviceAdminSample = new ComponentName(Controller.this, LockScreenActivity.class); active = mDPM.isAdminActive(mDeviceAdminSample); System.out.println("valuve of active"+active); if(active){ System.out.println("phone Unlocked********"); mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); mLock = mKeyGuardManager.newKeyguardLock("Controller.this"); mLock.disableKeyguard(); } System.out.println("TapUnlock++++++++++"); // getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // TapUnlock =0; reg=1; Tap=0; } } }// sensor changed }// Type proximity }; \[/code\]For first time lock and unlock screen code working and I am getting value of \[code\]active is true\[/code\] but for second time the value of \[code\]active is false\[/code\]. why it is false for second time? If I get the values of active is true always then i might get lock and unlock screen properly. what to do to get \[code\]active = true\[/code\] always.
 
Top