/**
* 实现类似微信语音通话屏幕防触摸功能
* 主要通过两种方式实现:
* 1.api21以上 并且支持PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK 通过 接近屏幕熄灭的电源管理器 来实现
* 该电源管理器在接近手机时 会自动熄灭屏幕 远离时 自动开启屏幕
* 2.其余情况 通过 接近传感器 来实现,接近手机时 将屏幕亮度降到最低同时禁止事件传递 远离时手机时 将屏幕亮度恢复同时恢复事件传递(实现伪熄屏)
*
*/
public class AudioActivity extends Activity {
private final String TAG="AudioActivity";
private PowerManager.WakeLock mWakeLock;
private KeyguardManager mKeyguardManager = null;
private KeyguardLock mKeyguardLock = null;
public boolean isDownHome;
private SensorManager mSensorManager;
private SensorEventListener mSensorEventListener;
private Sensor mProximitySensor;
private PowerManager mPowerManager;
private float defaultScreenBrightness;
private boolean isProximity;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
openKeyguardLock();
preventTouch();
super.onCreate(savedInstanceState);
}
private void openKeyguardLock() {
PowerManager.WakeLock wakeLock = mPowerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "wakeLock");
if (!wakeLock.isHeld()) wakeLock.acquire();
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock("");
mKeyguardLock.disableKeyguard();
wakeLock.release();
}
private void preventTouch() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mPowerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "CALL_ACTIVITY" + "#"
+ getClass().getName());
if (!mWakeLock.isHeld())
mWakeLock.acquire();
} else {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
defaultScreenBrightness = attributes.screenBrightness;
mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
float[] values = event.values;
if (values.length > 0) {
if (values[0] == 0.0) {
Log.i(TAG, "贴近手机");
attributes.screenBrightness = 0;
isProximity = true;
} else {
Log.i(TAG, "远离手机");
attributes.screenBrightness = defaultScreenBrightness;
isProximity = false;
}
getWindow().setAttributes(attributes);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
List sensorList = mSensorManager.getSensorList(Sensor.TYPE_PROXIMITY);
if (sensorList.size() > 0)
mProximitySensor = sensorList.get(0);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return isProximity || super.dispatchTouchEvent(ev);
}
/**
* 重新上锁 释放电源管理器
*/
private void releaseWakeLock() {
try {
if (mKeyguardLock != null) {
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}
} catch (Exception e) {
CustomLog.e("AndroidRuntime", e.toString());
}
}
@Override
protected void onResume() {
super.onResume();
if (mSensorEventListener != null)
mSensorManager.registerListener(mSensorEventListener, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
if (mSensorEventListener != null)
mSensorManager.unregisterListener(mSensorEventListener);
}
@Override
protected void onDestroy() {
releaseWakeLock();
super.onDestroy();
}
}
需要权限
如有问题欢迎交流