我知道这与这个问题相似 android: turn off screen when close to face 和许多其他人。但我找不到任何有效的解决方案。
所以我有一个播放音频的程序。我听接近传感器:
@Override
public void onSensorChanged(SensorEvent event) {
if(event.values[0] == 0.0){
//phone close to face
playAudio();
}else{
//phone away
stopAudio();
}
我已经使用此代码在靠近face时禁用creen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);
但我不知道怎么回来:) 我试过了:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1f; //tried all (1, 100, -1, 0)
getWindow().setAttributes(params);
有人建议:
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
但没有任何作用。当我将手机远离脸部时,音频停止。所以活动没有暂停。
答案 0 :(得分:0)
将先前的屏幕亮度值保存在变量中,然后使用恢复它 setAttributes
例如:
// Before changing brightness to 0
WindowManager.LayoutParams params = getWindow().getAttributes();
float oldScreenBrightness = params.screenBrightness; // Save the brightness param
//... Your code
// When u want to restore the brightness
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = oldScreenBrightness;
getWindow().setAttributes(params);