简体   繁体   English

使用接近传感器锁定和解锁主屏幕

[英]Using proximity sensor lock and unlock a home screen

I am doing one application using proximity sensor in android. 我正在使用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 提前Thx

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. 您需要保持应用打开状态以使其在手机锁定时能够正常工作。为此,您需要在执行此代码之前获得用户的许可并更改手机设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM