PROXIMITY_SCREEN_OFF_WAKE_LOCK not working with Samsung

眉间皱痕 提交于 2020-01-16 18:07:51

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/55488799/proximity-screen-off-wake-lock-not-working-with-samsung

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!