Using proximity sensor lock and unlock a home screen

无人久伴 提交于 2019-12-30 11:08:22

问题


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:

@Override
 public void onSensorChanged(SensorEvent event) {
 // TODO Auto-generated method stub
  if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){

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

         // unlock

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

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


回答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.



来源:https://stackoverflow.com/questions/8982819/using-proximity-sensor-lock-and-unlock-a-home-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!