17

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?

mtmurdock
  • 12,756
  • 21
  • 65
  • 108

4 Answers4

26

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.

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
Dre
  • 4,298
  • 30
  • 39
  • 1
    That is a lot to sift through... im afraid i wasnt able to find much there, unless you can point me to some more specific parts – mtmurdock Jun 10 '10 at 22:29
  • There, full info added -- didn't want to before in case information from open source code would cause an issue. – Dre Jun 10 '10 at 23:08
  • 3
    Note that with Android 4.2.1 the method `getSupportedWakeLockFlags` no longer exists and you need to use `isWakeLockLevelSupported`. http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/os/PowerManager.java/?v=diff&id2=4.1.2_r1 – Muzikant Mar 03 '14 at 08:06
  • 2
    `PROXIMITY_SCREEN_OFF_WAKE_LOCK` is public as of API level 21 (Lollipop). – Eliezer Jan 02 '15 at 00:08
7

as of API level 21 (Lollipop) you can get proximity wake lock this just like that:

if(powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
        wakeLock.setReferenceCounted(false);
        return wakeLock;
    } else {
        return null;
    }
}

then it is up to you to acquire and release the lock.

PS: PowerManager#getSupportedWakeLockFlags was hidden, but now exists nomore. They have invented isWakeLockLevelSupported instead.

guness
  • 6,336
  • 7
  • 59
  • 88
2

Probably you don't need it anymore but for the ones that are interested in code you could have a look at my SpeakerProximity project at http://code.google.com/p/speakerproximity/

rac2030
  • 507
  • 3
  • 10
1

What you are seeing is the use of a proximity sensor. For devices that have one, you access it through SensorManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yea i was looking into that, but i was having trouble figuring out the actual implementation. Have you seen any examples of how to use it? – mtmurdock Jun 11 '10 at 00:27
  • @mtmurdock: no, sorry, haven't seen any. I just know that's the sensor in question. – CommonsWare Jun 11 '10 at 00:37