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

发布于 2024-12-28 09:46:53 字数 1117 浏览 0 评论 0原文

我正在android中使用接近传感器做一个应用程序。当传感器发生变化时,它应该锁定手机,当使用相同的传感器锁定手机时,它应该解锁手机。要锁定手机,我使用双击机制。只需轻轻一按即可锁定。我的代码如下:

@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();
    }
}

解锁代码仅在第一次点击时有效。我需要它应该在手机锁定后执行,但它不起作用。如何做到这一点? 提前谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2025-01-04 09:46:54

当手机锁定时,您的应用程序会进入待机模式,因此编写的代码不起作用。您需要保持应用程序打开才能在手机锁定时正常工作。为此,您需要获得用户的许可并在执行此代码之前更改手机设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文