有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java保存到手机中的文件

这是获取陀螺仪和加速度计读数的代码;我需要将这些数据存储在智能手机的文件中

获得加速度计和陀螺仪的读数。它们会显示在我的应用程序上。然而,我无法正确地添加延迟-导致效率低下的结果。我应该如何添加延迟以获得期望的结果

package com.example.sensor;

import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.content.Context;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private static final String TAG = "MainActivity";

    private SensorManager sensorManager;
    private Sensor accelerometer, mGyro;

    TextView xValue, yValue, zValue, xGyroValue, yGyroValue, zGyroValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        xValue = (TextView) findViewById(R.id.xValue);
        yValue = (TextView) findViewById(R.id.yValue);
        zValue = (TextView) findViewById(R.id.zValue);

        xGyroValue = (TextView) findViewById(R.id.xGyroValue);
        yGyroValue = (TextView) findViewById(R.id.yGyroValue);
        zGyroValue = (TextView) findViewById(R.id.zGyroValue);

        Log.d(TAG, "onCreate: Initializing Sensor Services");
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (accelerometer != null) {
            sensorManager.registerListener(MainActivity.this, accelerometer, SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
            Log.d(TAG, "onCreate: Registered Accelerometer Listener");
        } else {
            xValue.setText("Accelerometer not supported");
            yValue.setText("Accelerometer not supported");
            zValue.setText("Accelerometer not supported");
        }
        mGyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        if (mGyro != null) {
            sensorManager.registerListener(MainActivity.this, mGyro, SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
            Log.d(TAG, "onCreate: Registered Gyroscope Listener");
        } else {
            xGyroValue.setText("Gyro not supported");
            yGyroValue.setText("Gyro not supported");
            zGyroValue.setText("Gyro not supported");

        }
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }


    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        Sensor sensor = sensorEvent.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            Log.d(TAG, "onSensorChanged: X:" + sensorEvent.values[0] + "Y:" + sensorEvent.values[1] + "Z:" + sensorEvent.values[2]);

            xValue.setText("xValue:" + sensorEvent.values[0]);
            yValue.setText("yValue:" + sensorEvent.values[1]);
            zValue.setText("zValue:" + sensorEvent.values[2]);

        } else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            xGyroValue.setText("xGValue:" + sensorEvent.values[0]);
            yGyroValue.setText("yGValue:" + sensorEvent.values[1]);
            zGyroValue.setText("zGValue:" + sensorEvent.values[2]);


        }

    }

}

请帮忙


共 (0) 个答案