使用接近传感器锁定和解锁主屏幕
我正在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.