【问题标题】:App freezes while recording accelerometer data: Android记录加速度计数据时应用程序冻结:Android
【发布时间】:2013-01-23 19:37:22
【问题描述】:

我只是想记录加速度计数据并通过单击按钮在 sdcard 中写入文件。在我的 Nexus S(4.1.2)中运行应用程序时,它会在一分钟后冻结。我尝试在 Galaxy Nexus 手机中运行它并且运行顺畅。由于某些原因,我必须在 Nexus S 中工作。谁能建议我崩溃的原因。我试图查看日志,但它没有通过任何错误消息。

这是我的代码:

final SensorEventListener mySensorEventListener = new SensorEventListener() { 
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            xAxis_lateralA = sensorEvent.values[0]; 
            yAxis_longitudinalA = sensorEvent.values[1]; 
            zAxis_verticalA = sensorEvent.values[2]; // TODO apply the acceleration changes to your application.
            textView.append("\nACC_x = "+ xAxis_lateralA + ", ACC_y = "+yAxis_longitudinalA+ ", ACC_z = " + zAxis_verticalA);

            acc += "\n"+miliSec()+", "+xAxis_lateralA + ", "+ yAxis_longitudinalA+", "+zAxis_verticalA;

            try {
                File myFile = new File("/sdcard/acc.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                myOutWriter.append(acc);
                myOutWriter.close();
                fOut.close();

            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};

startButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(getBaseContext(),"Done writing SD 'acc.txt'",Toast.LENGTH_SHORT).show();
        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
        int sensorType = Sensor.TYPE_ACCELEROMETER; 
        sm.registerListener(mySensorEventListener,sm.getDefaultSensor(sensorType), SensorManager.SENSOR_DELAY_NORMAL);

    }// onClick
}); // btnWriteSDFile 


stopButton = (Button) findViewById(R.id.stopButton);

stopButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        int sensorType = Sensor.TYPE_ACCELEROMETER;
        sm.unregisterListener(mySensorEventListener, sm.getDefaultSensor(sensorType));
        Toast.makeText(getBaseContext(), "Stopped Recording",Toast.LENGTH_SHORT).show();
        finish();

    }// onClick
}); // btnstopButton

【问题讨论】:

  • 文件操作似乎是在 UI Thread 上完成的。这可能是挂起的原因。考虑为此目的使用 Thread 或 AsyncTask。
  • 好的,谢谢你的建议。我试一试。
  • 好吧,我尝试添加一个子类并在acc+="\n"+miliSec()+", "+xAxis_lateralA + ", "+ yAxis_longitudinalA+", "+zAxis_verticalA;.. 之后立即调用执行方法。但感觉没有任何区别。
  • 如果你把acc+=后面的try catch拿出来放到doInBackground ()里,还是很慢,看来你又有问题了。 LogCat 会发出任何警告吗?
  • 这正是我所做的。刚刚收到这个 - I/InputDispatcher( 257): Application is not responding: AppWindowToken{41765198 token=Token{4144b938 ActivityRecord{417c07e8 com.os.noisewriter/.NoiseWriterActivity}}} - Window{41766d20 com.os.noisewriter/com.os.noisewriter.NoiseWriterActivity paused=false}. It has been 5005.4ms since event, 5005.0ms since wait started. Reason: Waiting because the focused window has not finished processing the input events that were previously delivered to it.

标签: android accelerometer nexus-s


【解决方案1】:

问题是由于您将值写入文本文件的方式。

每次读取传感器时,您都在打开/写入/关闭文件。

即使传感器读取频率为 50Hz,也需要大量计算,以 50 次/秒的速度在文本中写入这些数据效率不高。

使用BufferedWriter,性能更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多