Si puede ver el código fuente abierto sin causar problemas, consulte la fuente del Android Phone Application. Específicamente src/com/android/phone/PhoneApp.java y src/com/android/phone/InCallScreen.java.
De src/com/android/teléfono/PhoneApp.java:
//Around line 519
// Wake lock used to control proximity sensor behavior.
if ((pm.getSupportedWakeLockFlags()
& PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) != 0x0) {
mProximityWakeLock = pm.newWakeLock(
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
LOG_TAG);
}
....
// Around line 1334
if (((state == Phone.State.OFFHOOK) || mBeginningCall)&& !screenOnImmediately) {
// Phone is in use! Arrange for the screen to turn off
// automatically when the sensor detects a close object.
if (!mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
mProximityWakeLock.acquire();
} else {
if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
}
} else {
// Phone is either idle, or ringing. We don't want any
// special proximity sensor behavior in either case.
if (mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
// Wait until user has moved the phone away from his head if we are
// releasing due to the phone call ending.
// Qtherwise, turn screen on immediately
int flags =
(screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
mProximityWakeLock.release(flags);
}
}
Además, si nos fijamos en el código de la clase PowerManager, se documenta PROXIMITY_SCREEN_OFF_WAKE_LOCK (pero oculta) y debe hacer lo que quiera (No estoy seguro de qué nivel de API funciona esto, sin embargo), pero no en la tabla por alguna razón.
/**
* Wake lock that turns the screen off when the proximity sensor activates.
* Since not all devices have proximity sensors, use
* {@link #getSupportedWakeLockFlags() getSupportedWakeLockFlags()} to determine if
* this wake lock mode is supported.
*
* {@hide}
*/
public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = WAKE_BIT_PROXIMITY_SCREEN_OFF;
Si no tiene miedo de utilizar una función potencial no documentada, debería hacer exactamente lo que necesita.
Sí, estaba investigando eso, pero estaba teniendo problemas para descubrir la implementación real. ¿Has visto algún ejemplo de cómo usarlo? – mtmurdock
@mtmurdock: no, lo siento, no he visto ninguno. Solo sé que ese es el sensor en cuestión. – CommonsWare