【体感手势】口袋模式或者防误触

2024-02-09 21:38

本文主要是介绍【体感手势】口袋模式或者防误触,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

口袋模式或者防止误触

带有黑屏手势功能的手机,一般都需要进行一个防误触的判断。防止手机放在口袋里或者包包(一些导电介质的东西可能带来辅助效果,比如钥匙)里别误触发点亮屏幕,甚至是打出电话。

原理

接近式传感器的靠近与远离功能。
可以参考我们手机打电话时,耳朵贴近手机的时候,往往屏幕是自动息屏,离开耳朵一小段距离,屏幕又亮起的这个功能。同理和我们放在口袋中一个意思。比较严格的防误触可能会加上Acc重力传感器进行判断

调用方法

原理->基于接近式传感器的远近识别

    private boolean type_proximity = true;private ProximitySensorManager mProximitySensorManager;private void pocketDetector(boolean isEnable){if (isEnable && (mProximitySensorManager == null)) {mProximitySensorManager = new ProximitySensorManager(mContext, new ProximitySensorManager.Listener() {@Overridepublic void onNear() {// 靠近口袋事件type_proximity = true;}@Overridepublic void onFar() {// 远离口袋事件type_proximity = false;}});mProximitySensorManager.enable();} else if(!isEnable && (mProximitySensorManager != null)){mProximitySensorManager.disable(false);mProximitySensorManager = null;}}  

其中 ProximitySensorManager.java 是系统自带的源码直接粘贴过来,直接使用即可

系统工具类ProximitySensorManager

package com.android.server.policy;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;/*** Manages the proximity sensor and notifies a listener when enabled.*/
public class ProximitySensorManager {/*** Listener of the state of the proximity sensor.* <p>* This interface abstracts two possible states for the proximity sensor, near and far.* <p>* The actual meaning of these states depends on the actual sensor.*/public interface Listener {/** Called when the proximity sensor transitions from the far to the near state. */public void onNear();/** Called when the proximity sensor transitions from the near to the far state. */public void onFar();}public static enum State {NEAR, FAR}private final ProximitySensorEventListener mProximitySensorListener;/*** The current state of the manager, i.e., whether it is currently tracking the state of the* sensor.*/private boolean mManagerEnabled;/*** The listener to the state of the sensor.* <p>* Contains most of the logic concerning tracking of the sensor.* <p>* After creating an instance of this object, one should call {@link #register()} and* {@link #unregister()} to enable and disable the notifications.* <p>* Instead of calling unregister, one can call {@link #unregisterWhenFar()} to unregister the* listener the next time the sensor reaches the {@link State#FAR} state if currently in the* {@link State#NEAR} state.*/private static class ProximitySensorEventListener implements SensorEventListener {private static final float FAR_THRESHOLD = 5.0f;private final SensorManager mSensorManager;private final Sensor mProximitySensor;private final float mMaxValue;private final Listener mListener;/*** The last state of the sensor.* <p>* Before registering and after unregistering we are always in the {@link State#FAR} state.*/private State mLastState;/*** If this flag is set to true, we are waiting to reach the {@link State#FAR} state and* should notify the listener and unregister when that happens.*/private boolean mWaitingForFarState;public ProximitySensorEventListener(SensorManager sensorManager, Sensor proximitySensor,Listener listener) {mSensorManager = sensorManager;mProximitySensor = proximitySensor;mMaxValue = proximitySensor.getMaximumRange();mListener = listener;// Initialize at far state.mLastState = State.FAR;mWaitingForFarState = false;}@Overridepublic void onSensorChanged(SensorEvent event) {// Make sure we have a valid value.if (event.values == null) return;if (event.values.length == 0) return;float value = event.values[0];// Convert the sensor into a NEAR/FAR state.State state = getStateFromValue(value);synchronized (this) {// No change in state, do nothing.if (state == mLastState) return;// Keep track of the current state.mLastState = state;// If we are waiting to reach the far state and we are now in it, unregister.if (mWaitingForFarState && mLastState == State.FAR) {unregisterWithoutNotification();}}// Notify the listener of the state change.switch (state) {case NEAR:mListener.onNear();break;case FAR:mListener.onFar();break;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// Nothing to do here.}/** Returns the state of the sensor given its current value. */private State getStateFromValue(float value) {// Determine if the current value corresponds to the NEAR or FAR state.// Take case of the case where the proximity sensor is binary: if the current value is// equal to the maximum, we are always in the FAR state.return (value > FAR_THRESHOLD || value == mMaxValue) ? State.FAR : State.NEAR;}/*** Unregister the next time the sensor reaches the {@link State#FAR} state.*/public synchronized void unregisterWhenFar() {if (mLastState == State.FAR) {// We are already in the far state, just unregister now.unregisterWithoutNotification();} else {mWaitingForFarState = true;}}/** Register the listener and call the listener as necessary. */public synchronized void register() {// It is okay to register multiple times.mSensorManager.registerListener(this, mProximitySensor, SensorManager.SENSOR_DELAY_UI);// We should no longer be waiting for the far state if we are registering again.mWaitingForFarState = false;}public void unregister() {State lastState;synchronized (this) {unregisterWithoutNotification();lastState = mLastState;// Always go back to the FAR state. That way, when we register again we will get a// transition when the sensor gets into the NEAR state.mLastState = State.FAR;}// Notify the listener if we changed the state to FAR while unregistering.if (lastState != State.FAR) {mListener.onFar();}}private void unregisterWithoutNotification() {mSensorManager.unregisterListener(this);mWaitingForFarState = false;}}public ProximitySensorManager(Context context, Listener listener) {SensorManager sensorManager =(SensorManager) context.getSystemService(Context.SENSOR_SERVICE);Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);if (proximitySensor == null) {// If there is no sensor, we should not do anything.mProximitySensorListener = null;} else {mProximitySensorListener =new ProximitySensorEventListener(sensorManager, proximitySensor, listener);}}/*** Enables the proximity manager.* <p>* The listener will start getting notifications of events.* <p>* This method is idempotent.*/public void enable() {if (mProximitySensorListener != null && !mManagerEnabled) {mProximitySensorListener.register();mManagerEnabled = true;}}/*** Disables the proximity manager.* <p>* The listener will stop receiving notifications of events, possibly after receiving a last* {@link Listener#onFar()} callback.* <p>* If {@code waitForFarState} is true, if the sensor is not currently in the {@link State#FAR}* state, the listener will receive a {@link Listener#onFar()} callback the next time the sensor* actually reaches the {@link State#FAR} state.* <p>* If {@code waitForFarState} is false, the listener will receive a {@link Listener#onFar()}* callback immediately if the sensor is currently not in the {@link State#FAR} state.* <p>* This method is idempotent.*/public void disable(boolean waitForFarState) {if (mProximitySensorListener != null && mManagerEnabled) {if (waitForFarState) {mProximitySensorListener.unregisterWhenFar();} else {mProximitySensorListener.unregister();}mManagerEnabled = false;}}
}

结论

实践证明系统的提供的判断误触的工具栏非常好用,简单粗暴又省电,还有稳定得没亲妈

这篇关于【体感手势】口袋模式或者防误触的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/695345

相关文章

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素

《x86汇编语言:从实模式到保护模式》视频来了

《x86汇编语言:从实模式到保护模式》视频来了 很多朋友留言,说我的专栏《x86汇编语言:从实模式到保护模式》写得很详细,还有的朋友希望我能写得更细,最好是覆盖全书的所有章节。 毕竟我不是作者,只有作者的解读才是最权威的。 当初我学习这本书的时候,只能靠自己摸索,网上搜不到什么好资源。 如果你正在学这本书或者汇编语言,那你有福气了。 本书作者李忠老师,以此书为蓝本,录制了全套视频。 试

利用命令模式构建高效的手游后端架构

在现代手游开发中,后端架构的设计对于支持高并发、快速迭代和复杂游戏逻辑至关重要。命令模式作为一种行为设计模式,可以有效地解耦请求的发起者与接收者,提升系统的可维护性和扩展性。本文将深入探讨如何利用命令模式构建一个强大且灵活的手游后端架构。 1. 命令模式的概念与优势 命令模式通过将请求封装为对象,使得请求的发起者和接收者之间的耦合度降低。这种模式的主要优势包括: 解耦请求发起者与处理者

springboot实战学习(1)(开发模式与环境)

目录 一、实战学习的引言 (1)前后端的大致学习模块 (2)后端 (3)前端 二、开发模式 一、实战学习的引言 (1)前后端的大致学习模块 (2)后端 Validation:做参数校验Mybatis:做数据库的操作Redis:做缓存Junit:单元测试项目部署:springboot项目部署相关的知识 (3)前端 Vite:Vue项目的脚手架Router:路由Pina:状态管理Eleme

状态模式state

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/state 在一个对象的内部状态变化时改变其行为, 使其看上去就像改变了自身所属的类一样。 在状态模式中,player.getState()获取的是player的当前状态,通常是一个实现了状态接口的对象。 onPlay()是状态模式中定义的一个方法,不同状态下(例如“正在播放”、“暂停

软件架构模式:5 分钟阅读

原文: https://orkhanscience.medium.com/software-architecture-patterns-5-mins-read-e9e3c8eb47d2 软件架构模式:5 分钟阅读 当有人潜入软件工程世界时,有一天他需要学习软件架构模式的基础知识。当我刚接触编码时,我不知道从哪里获得简要介绍现有架构模式的资源,这样它就不会太详细和混乱,而是非常抽象和易

使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统

引言 在企业级应用开发中,数据库操作是非常重要的一环。Spring Data JPA提供了一种简化的方式来进行数据库交互,它使得开发者无需编写复杂的JPA代码就可以完成常见的CRUD操作。此外,设计模式如单例模式可以帮助我们更好地管理和控制对象的创建过程,从而提高系统的性能和可维护性。本文将展示如何结合Spring Boot、Spring Data JPA以及单例模式来构建一个基本的库存管理系统