问题
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