Reputation: 2771
I'm updating my application based on users huge request. My app turns the screen ON after something happens and now I'm integrating "pocket mode" functionality. So basically if the user has phone or device in his/her pocket I would like to detect this via proximity sensor and act based on this. But I'm experiencing a lot of troubles..
So I'm registering sensor and everything as usual. One thing I would like to point out is that I'm telling the PowerManager object to register as Proximity_Screen_Off_Wake_Lock. That means that every time the screen will go automatically off when something near is detected.
powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "ProximityScreenOff");
Basically when device is on the table and I move finger to the sensor the screen turns off as expected.
The problem starts when my activity launches and I'm already holding the finger on the sensor (or is in pocket - it's the same). So sensor doesn't detect anything that is already near phone. If I move the finger a little bit away the screen will turn ON again.
Is there anything that I could do, so I would get my desired behavior - that is, turning the screen OFF when phone is already in pocket?
Upvotes: 0
Views: 408
Reputation: 1
You can manage this using boolean variable.
That is if near event of onSensorChanged called, Then n then only far event called.
boolean isNearCalled =false;
void onSensorChanged() {
if(near sensitivity) {
isNearCalled=true;
} else if(Distance sensitivity && isNearCalled) {
// Do stuff for far like screen off
isNearCalled= false;
}
}
Upvotes: 0