Help with PROXIMITY_SCREEN_OFF_WAKE_LOCK in Androi

2020-07-10 11:06发布

I've enabled the proximity wakelock in my app, and it turns off the screen when the proximity sensor detects something. But there is a problem when the screen wakes back up -- it goes to the lockscreen, not my app. This happens regardless of the time that the screen was off (even if the sensor is cleared after a few seconds). Here's the code I used:

int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32;    
mProximityWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
if(!mProximityWakeLock.isHeld()){
    mProximityWakeLock.acquire();
}

Is there any way to correct that behavior?

7条回答
【Aperson】
2楼-- · 2020-07-10 11:20

If it is appropriate for your app you can use

getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_IGNORE_CHEEK_PRESSES);
查看更多
趁早两清
3楼-- · 2020-07-10 11:26
private WakeLock mProximityWakeLock= null;

mProximityWakeLock= pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP , LOG_TAG);

Acquiring lock when screen needed to turn on and releasing when needed to off i.e. reverse to above wakeLock will work fine.

查看更多
时光不老,我们不散
4楼-- · 2020-07-10 11:32

You can dismiss the lock screen, if it is not a secure one, using:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

You can either call it on creation to prevent the lock screen from ever appearing or when you need to. I use it in combination with:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Which does not seem to interfere with the proximity lock.

查看更多
淡お忘
5楼-- · 2020-07-10 11:33

Besides wakeLock from PowerManager you must create KeyGuard lock like this:

private WakeLock mWakeLock = null;
private KeyguardLock mKeyguardLock = null;

public void enableProximitySensor() {
  PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
  mWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
  mWakeLock.acquire();

  KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
  mKeyguardLock = mManager.newKeyguardLock( KEYGUARD_TAG );
}

public void disableProximitySensor() {
  if ( null != mWakeLock ) {
    mWakeLock.release();
    mWakeLock = null;
  }
  if ( null != mKeyguardLock ) {
    mKeyguardLock.disableKeyguard();
    mKeyguardLock = null;
  }
}

Create this lock only for the same time as you acquire proximity wake lock and lock screen will work all other time in your application.

查看更多
小情绪 Triste *
6楼-- · 2020-07-10 11:37

If you're using mProximityWakeLock.release();, try using mProximityWakeLock.release(1);

The API reference is also hidden, but you can look at the source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/os/PowerManager.java#PowerManager.WakeLock.release%28int%29

查看更多
冷血范
7楼-- · 2020-07-10 11:40

How did u manage to turn back the screen on? I managed to turn the screen off, when the sensor is covered, using:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
etWindow().setAttributes(lp);

and after this, "releasing" or "reseting" the cover from/on the sensor, won't execute the other part of the code:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
etWindow().setAttributes(lp);
查看更多
登录 后发表回答