PROXIMITY_SCREEN_OFF_WAKE_LOCK无法与三星配合使用 [英] PROXIMITY_SCREEN_OFF_WAKE_LOCK not working with Samsung

查看:228
本文介绍了PROXIMITY_SCREEN_OFF_WAKE_LOCK无法与三星配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用接近传感器,而android中的webRtc调用使用设备传感器来打开/关闭屏幕.它可以在大多数设备上正常运行,但不能在三星设备上运行.当Sensor关闭屏幕时,将调用该活动的onStop().以下是我正在使用的代码:

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

推荐答案

尽量不要直接使用SensorManager.它帮助我使用了三星S10e.

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

这篇关于PROXIMITY_SCREEN_OFF_WAKE_LOCK无法与三星配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆