type_linear_acceleration

时间:2016-11-22 02:09:21

标签: android accelerometer

我正在创建一个记录加速度计读数的应用程序。当我输入TYPE_ACCELEROMETER时,它可以完美地记录原始加速度计数据。但是,当我输入TYPE_LINEAR_ACCELERATION时,它无法返回任何值。该应用程序不会给我一个错误或退出。我认为我必须要么使用不支持TYPE_LINEAR_ACCELERATION的设备,要么我的清单中没有所有必要的权限。

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

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

        currentX = (TextView) findViewById(R.id.currentX);
        currentY = (TextView) findViewById(R.id.currentY);
        currentZ = (TextView) findViewById(R.id.currentZ);


    }

public void onSensorChanged(SensorEvent event) {

            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];


            currentX.setText(Float.toString(x));
            currentY.setText(Float.toString(y));
            currentZ.setText(Float.toString(z));

            try {
                writer.write(x + "," + y + "," + z + "\n");
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    };
public void Recordbutton(View view) {

        if (recordbuttonstatus) {
            playBtn.setEnabled(true);
            recordbuttonstatus = false;
            startBtn.setText(getString(R.string.stoprecording));
            sensorManager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);

            Thread accelerometerrecord = new Thread(new Runnable() {

                @Override
                        public void run(){
                File accoutputFile = new File(Environment.getExternalStorageDirectory(), "accelerometervals.txt");
                try {
                    accoutputFile.createNewFile();
                } catch (IOException e){
                    e.printStackTrace();
                }
                    try {
                        writer = new FileWriter(accoutputFile, false); /*When false, the file is overwritten, when true, the file is appended.*/
                    } catch (IOException e) {
                        Log.e(LOG_TAG2, "failed");
                    }
                }
            });

            accelerometerrecord.start();

} else {
            startBtn.setText(getString(R.string.newrecording));
            recordbuttonstatus = true;
            sensorManager.unregisterListener(accelListener);

        }
    }

 protected void onPause() {
        super.onPause();

        sensorManager.unregisterListener(accelListener);

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    }

public void onStop() {
        super.onStop();
        sensorManager.unregisterListener(accelListener);

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

谢谢大家。事实证明我的手机没有线性加速计传感器。