【问题标题】:Android Wear detecting wrist tilt in watch face/appAndroid Wear 在表盘/应用程序中检测手腕倾斜
【发布时间】:2016-11-27 22:47:52
【问题描述】:

有没有办法在表盘处于活动状态时检测“手腕倾斜以唤醒屏幕”动作?我希望我的表盘能够检测到此操作,以便通过再次倾斜手表来延迟屏幕超时(例如)。

【问题讨论】:

    标签: android wear-os android-wear-2.0


    【解决方案1】:

    大多数基于 Android 的设备都有内置传感器,用于测量运动、方向和各种环境条件。这些传感器能够提供高精度和准确度的原始数据。您可以使用运动传感器来检测倾斜、晃动、旋转或摆动等。

    支持三大类传感器:

    • 运动传感器
    • 环境传感器
    • 位置传感器

    这是一个感应重力和加速度计的示例代码:

    private SensorManager mSensorManager;
    private Sensor mSensor;
    
    ...
    
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = null;
    
    if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){
      List<Sensor> gravSensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);
      for(int i=0; i<gravSensors.size(); i++) {
        if ((gravSensors.get(i).getVendor().contains("Google Inc.")) &&
           (gravSensors.get(i).getVersion() == 3)){
          // Use the version 3 gravity sensor.
          mSensor = gravSensors.get(i);
        }
      }
    }
    if (mSensor == null){
      // Use the accelerometer.
      if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      }
      else{
        // Sorry, there are no accelerometers on your device.
        // You can't play this game.
      }
    }
    

    有关传感器管理器的更多信息,请查看:https://developer.android.com/guide/topics/sensors/sensors_overview.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多