I am doing one application using proximity sensor in android. when any object(finger) comes near to proximity sensor it is working fine. But I want to work application when the object should come second time near to proximity within one second of time. For example my application will lock a phone when finger comes near to proximity on first time. I want lock a phone when same object come near to proximity within second of time. my code is like below:
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(count >=0){
System.out.println("inside on sensor changed*****");
ProximityReading.setText("Proximity Sensor Reading:"
+ String.valueOf(event.values[0]));
count++;
System.out.println("inside count*****"+count);
}
if(count == 1){
System.out.println("inside second if *****");
System.out.println("inside second if*****"+count);
count = 0;
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this,
LockScreenActivity.class);
active = mDPM.isAdminActive(mDeviceAdminSample);
System.out.println("valuve of active"+active);
if(active){
System.out.println("***inside if");
mDPM.lockNow();
System.out.println("***inside active after if");
}
}
But it is not working :(. plz help me and how to set a timer between first and second time object comes to near. thax in advance
When you capture the first proximity event, save the timestamp using
System.currentTimeInMillis(). Do the same for the second proximity event and compare the timestamps.