手表靠近嘴时如何启动应用程序
How to launch the application when the watch is near mouth
我想开发一个 android 佩戴应用程序,当手表靠近嘴巴时可以听取用户的声音。我怎样才能检测到这种运动?
我设计了一个具有类似功能的 covid-19 wear 应用程序来防止用户触摸他们的 mouth/eyes(面部区域)。
您可以注册重力传感器侦听器并检测用户何时将手举向 mouth/head 区域。
private SensorManager mSensorManager;
//register in #onResume
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_NORMAL);
用户佩戴手表时,重力感应器测得的重力x-component在手朝下时为+9.8,在手朝上朝嘴时为-9.8。当手表戴在右手上时,这些值是相反的。由于向上或向下的值可能不精确,您可以留出一些空间,因此您可以使用 GRAVITY THRESHOLD
of (7.0f
) 而不是 9.8。如果花费的时间小于 2000000000
纳秒的阈值,您也可以认为 up/down 移动成功:
// An movement to the mouth and down that takes more than 2 seconds will not be registered (in nanoseconds).
private static final long TIME_THRESHOLD_NS = TimeUnit.SECONDS.toNanos(2);
private static final float HAND_DOWN_GRAVITY_X_THRESHOLD = -.040f;
private static final float HAND_UP_GRAVITY_X_THRESHOLD = -.010f;
@Override
public void onSensorChanged(SensorEvent event) {
detectMouthTouched(event.values[0], event.timestamp);
}
private void detectMouthTouched(float xGravity, long timestamp) {
if ((xGravity <= HAND_DOWN_GRAVITY_X_THRESHOLD)
|| (xGravity >= HAND_UP_GRAVITY_X_THRESHOLD)) {
if (timestamp - mLastTime < TIME_THRESHOLD_NS) {
// Hand is down when yValue is negative.
onFaceTouched(xGravity <= HAND_DOWN_GRAVITY_X_THRESHOLD);
}
mLastTime = timestamp;
}
}
private void onFaceTouched(boolean handDown) {
if (mHandDown != handDown) {
mHandDown = handDown;
}
}
我想开发一个 android 佩戴应用程序,当手表靠近嘴巴时可以听取用户的声音。我怎样才能检测到这种运动?
我设计了一个具有类似功能的 covid-19 wear 应用程序来防止用户触摸他们的 mouth/eyes(面部区域)。 您可以注册重力传感器侦听器并检测用户何时将手举向 mouth/head 区域。
private SensorManager mSensorManager;
//register in #onResume
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_NORMAL);
用户佩戴手表时,重力感应器测得的重力x-component在手朝下时为+9.8,在手朝上朝嘴时为-9.8。当手表戴在右手上时,这些值是相反的。由于向上或向下的值可能不精确,您可以留出一些空间,因此您可以使用 GRAVITY THRESHOLD
of (7.0f
) 而不是 9.8。如果花费的时间小于 2000000000
纳秒的阈值,您也可以认为 up/down 移动成功:
// An movement to the mouth and down that takes more than 2 seconds will not be registered (in nanoseconds).
private static final long TIME_THRESHOLD_NS = TimeUnit.SECONDS.toNanos(2);
private static final float HAND_DOWN_GRAVITY_X_THRESHOLD = -.040f;
private static final float HAND_UP_GRAVITY_X_THRESHOLD = -.010f;
@Override
public void onSensorChanged(SensorEvent event) {
detectMouthTouched(event.values[0], event.timestamp);
}
private void detectMouthTouched(float xGravity, long timestamp) {
if ((xGravity <= HAND_DOWN_GRAVITY_X_THRESHOLD)
|| (xGravity >= HAND_UP_GRAVITY_X_THRESHOLD)) {
if (timestamp - mLastTime < TIME_THRESHOLD_NS) {
// Hand is down when yValue is negative.
onFaceTouched(xGravity <= HAND_DOWN_GRAVITY_X_THRESHOLD);
}
mLastTime = timestamp;
}
}
private void onFaceTouched(boolean handDown) {
if (mHandDown != handDown) {
mHandDown = handDown;
}
}