我只是想知道是否有可能/我从哪里开始测量 Android 设备的向上和向下移动。我需要它尽可能地准确。

我完全不知道在哪里看我已经看到了某些方法 here ,并且看着 these old questions 他们提到这很困难,但我想知道从那时起在较新版本的 android 中是否有任何改进。

下图是我希望手机移动的方向的额外示例。

android - 是否可以测量手机垂直移动的距离-LMLPHP

最佳答案

在开始解决之前,我会将解决方案拆分为小 Assets 并将难题放在解决方案中

  • 我们需要听手机传感器
  • 我们需要检查手机是否处于垂直位置
  • 我们需要制作一个时间计数器,垂直注册手机并向上计数
  • 我们需要在屏幕上显示时间

  • 对于第一步,我将在我们的类中实现 SensorEventListener,这将允许我们使用 onSensorChanged 方法。
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            listenToSensors(event);
        }
    }
    
    private void listenToSensors(SensorEvent event) {
        if (isPhoneVertical(event)) {
            timeCounter();
            if (mStatus) {
                mStartTime = getStartTime();
                mStatus = false;
            }
        } else {
            if (!mStatus) {
                mTotalTime = getDiffTime() + mTotalTime;
                mStatus = true;
            }
        }
    }
    

    对于第二步,我构建了一个名为 isPhoneVertical 的方法来检查我们的手机是否处于垂直状态,它主要是检查 y 轴。您可以通过更改 maxVertical 来更改陡峭程度。
    越小越不陡峭,0 意味着手机应该几乎 100% 垂直。对于我的测试,它设置为 3。
    private boolean isPhoneVertical(SensorEvent event) {
        float[] values = event.values;
        double y = values[1];
        // do not change this value
        double yAxisInitValue = 10.0;
        double verMargin = yAxisInitValue - maxVertical;
    
        return y >= verMargin;
    }
    

    对于第 3 步,我使用了一些方法来检查开始时间和停止时间,并更新以秒为单位跟踪时间的全局变量。
    private long getStartTime() {
        return System.currentTimeMillis() / 1000;
    }
    
    private long getDiffTime() {
        return getStartTime() - mStartTime;
    }
    

    对于第 4 步,我制作了一个常规的 runOnUiThread 来更新屏幕上的时间。
    private void timeCounter() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mView1.setText("Phone has been vertical for: " + getDiffTime() + " seconds");
                mView2.setText("The total time: " + (mTotalTime + getDiffTime()) + "");
            }
        });
    }
    

    就是说,此解决方案是为了说明如何实现此目标,我相信可以通过不同的方式来实现。但我想展示解决方案背后的逻辑。

    这是应用程序的屏幕截图,它计算手机每次垂直时的时间和垂直时的总时间。

    android - 是否可以测量手机垂直移动的距离-LMLPHP

    解决方案包括一些解释:

    MainActivity.java
    public class MainActivity extends Activity implements SensorEventListener {
        private SensorManager mSensorManager;
        private Sensor mAccelerometer;
        private TextView mView1, mView2;
        private long mStartTime;
        private long mTotalTime;
        private boolean mStatus = true;
        // less value her less steep, 0 means the phone should almost be 100% vertical
        // try it out
        private double maxVertical = 3.0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mView1 = (TextView) findViewById(R.id.textView1);
            mView2 = (TextView) findViewById(R.id.textView2);
    
            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                listenToSensors(event);
            }
        }
    
        private void listenToSensors(SensorEvent event) {
            if (isPhoneVertical(event)) {
                timeCounter();
                if (mStatus) {
                    mStartTime = getStartTime();
                    mStatus = false;
                }
            } else {
                if (!mStatus) {
                    mTotalTime = getDiffTime() + mTotalTime;
                    mStatus = true;
                }
            }
        }
    
        // This method return true only for specific phone orientation
        // y axis for vertical orientation
        private boolean isPhoneVertical(SensorEvent event) {
            float[] values = event.values;
            double y = values[1];
            // do not change this value
            double yAxisInitValue = 10.0;
            double verMargin = yAxisInitValue - maxVertical;
    
            return y >= verMargin;
        }
    
        private long getStartTime() {
            return System.currentTimeMillis() / 1000;
        }
    
        private long getDiffTime() {
            return getStartTime() - mStartTime;
        }
    
        // update steps in user interface
        private void timeCounter() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mView1.setText("Phone has been vertical for: " + getDiffTime() + " seconds");
                    mView2.setText("The total time: " + (mTotalTime + getDiffTime()) + "");
                }
            });
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mSensorManager.registerListener(this,
                    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            // if you want to collect data while mobile screen off, just disable the
            // following line, the app will still collecting sensor data
            mSensorManager.unregisterListener(this);
        }
    }
    

    Activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.maytham.verticaltravels.MainActivity">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:textSize="20sp"
            android:text="TextView1" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="57dp"
            android:textSize="20sp"
            android:text="TextView2" />
    </RelativeLayout>
    

    我也会留下一些链接供阅读。
  • https://developer.android.com/reference/android/hardware/SensorManager.html
  • http://www.livescience.com/40103-accelerometer-vs-gyroscope.html
  • https://physics.stackexchange.com/questions/41653/how-do-i-get-the-total-acceleration-from-3-axes
  • https://code.tutsplus.com/tutorials/how-to-recognize-user-activity-with-activity-recognition--cms-25851
  • http://www.vogella.com/tutorials/AndroidSensor/article.html
  • How to get Android phone orientation matching human orientation?

  • 如果您认为我可以添加更多信息来帮助您进一步解决您的问题,请告诉我。还不清楚您是否正在寻找步行检测/计数器,如果这是您感兴趣的事情,请查看以下答案/链接。
  • Activity Recognition Api
  • How to detect walking with Android accelerometer
  • how to calculate exact foot step count using accelerometer in android?
  • Detect Movement Accurately using Accelerometer in Android

  • 和 GitHub 源代码
  • https://github.com/bagilevi/android-pedometer
  • https://github.com/tartakynov/robowalk
  • 10-08 14:02
    查看更多