PROXIMITY_SCREEN_OFF_WAKE_LOCK not working with Samsung
Asked Answered
W

1

0

I am using the Proximity Sensor while webRtc call in android to turn screen on/off using device sensor. It is working perfectly in most of the devices but not in Samsung. When Sensor turns the screen off onStop() of the activity is called. Following is the code I am using :

@Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (mWakeLock != null) {
                    mWakeLock.release(1);
                    mWakeLock = null;
                }
                if (sensorEvent.values[0] >= -SENSOR_SENSITIVITY && sensorEvent.values[0] <= SENSOR_SENSITIVITY) {
                    mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
                    mWakeLock.acquire();
                } else {
                    //far
                    mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
                    mWakeLock.acquire();
                }
            }
        }
    }
Watterson answered 3/4, 2019 at 6:50 Comment(0)
C
2

Try not to use SensorManager directly. It helped me with the Samsung S10e.

class ProximityMgr(context: Context) {
    private val powerManager: PowerManager = context.getSystemService()!!
    private val wakeLock: PowerManager.WakeLock

    init {
        wakeLock = powerManager.newWakeLock(
                PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, 
                "lock:proximity_screen_off")
    }

    fun acquire() {
        if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
            if (wakeLock.isHeld) {
                wakeLock.release()
            }
            wakeLock.acquire(WAKE_LOCK_TIMEOUT_MS)
        } else {
            Log.w(TAG, "not supported")
        }
    }

    fun release() {
        if (wakeLock.isHeld)
            wakeLock.release()
    }

    companion object {
        private const val TAG = "ProximitySensor"
        private const val WAKE_LOCK_TIMEOUT_MS: Long = 2 * 3600 * 1000
    }
}
Cephalonia answered 20/11, 2019 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.