Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

android: turn off screen when close to face

My app allows the user to access their corporate voice mail. Normally, durring a phone call when the user holds the device up to their ear, the screen shuts off so they wont accidentally push buttons with their face. I would like to make my app do the same thing when the user is listening to their voice mail.

anyone know how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are allowed to look at open source code without causing yourself problems, check the source of the Android Phone Application. Specifically src/com/android/phone/PhoneApp.java and src/com/android/phone/InCallScreen.java.

From src/com/android/phone/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);
  }
}

Additionally, if you look at the code for the PowerManager class, PROXIMITY_SCREEN_OFF_WAKE_LOCK is documented (but hidden) and should do what you want ( I am not sure which API level this works for, however ) -- but not in the table for some reason.

/**
 * 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;

If you aren't afraid of using a potential undocumented feature, it should do exactly what you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...