2010-06-10 76 views
11

我的應用允許用戶訪問他們的企業語音郵件。通常,當用戶將設備放在耳邊時,通過撥打電話屏幕會關閉,這樣他們就不會不小心按下臉部按鈕。我想讓我的應用在用戶收聽他們的語音郵件時也做同樣的事情。android:在接近臉部時關閉屏幕

任何人都知道如何做到這一點?

回答

3

您所看到的是使用接近傳感器。對於有設備的設備,您可以通過SensorManager進行訪問。

+0

是的,我正在調查,但我很難搞清楚實際的實施。你有沒有看過如何使用它的例子? – mtmurdock 2010-06-11 00:27:54

+0

@mtmurdock:不,對不起,沒有見過。我只知道那是傳感器。 – CommonsWare 2010-06-11 00:37:35

20

如果您可以查看開源代碼而不會導致您的問題,請檢查Android Phone Application的來源。具體爲src/com/android/phone/PhoneApp.javasrc/com/android/phone/InCallScreen.java

從SRC/COM /安卓/電話/ 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); 
    } 
} 

此外,如果你看一下在電源管理器類的代碼,PROXIMITY_SCREEN_OFF_WAKE_LOCK是記錄(但隱藏)和應該做你想要什麼(然而,我不確定這是適用於哪個API級別),但由於某種原因,不在表格中。

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

如果您不害怕使用潛在的未記錄功能,它應該按照您的需要進行操作。

+0

這是很多通過篩選...我害怕我無法找到很多,除非你可以指點我一些更具體的部分 – mtmurdock 2010-06-10 22:29:40

+0

在那裏,全部信息 - 不希望以前的情況下信息從開源代碼會導致一個問題。 – Dre 2010-06-10 23:08:14

+0

該代碼中有私有API。 – Taranfx 2012-10-06 13:18:58

4

爲API級別21(棒棒堂),你可以得到接近喚醒鎖這就像這樣:

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; 
    } 
} 

那麼它是由你來獲得和釋放鎖。

PS:PowerManager#getSupportedWakeLockFlags已被隱藏,但現在存在nomore。相反,他們發明了isWakeLockLevelSupported

+1

儘管它表示API級別21是必需的,但它似乎是向後兼容的,因爲該標誌總是在那裏,只是隱藏起來。 – kotucz 2016-02-16 15:33:28