首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从加速度传感器写入文件- android

从加速度传感器写入文件- android
EN

Stack Overflow用户
提问于 2017-01-25 13:40:26
回答 1查看 511关注 0票数 1

我正面临一个技术人员代码问题。我想通过Toggle按钮将从TotalAccelerate获得的所有值写入我的.txt文件中。一旦我按下按钮,代码现在一次只写一个值。如何手动写入和停止写入文件?提前谢谢。

代码语言:javascript
复制
FileOutputStream fileOutputStream;
double TotalAccelerate; //I made it global variable
ArrayList<Double> list;

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

    list = new ArrayList<Double>();

     //for Accelermeter
    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensorText = (TextView) findViewById(R.id.sensor);
    accelermeter = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(this, accelermeter, SensorManager.SENSOR_DELAY_NORMAL);
    mDetector = new GestureDetectorCompat(this, new MyGestureListener());


    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File Root = Environment.getExternalStorageDirectory();
        File dir = new File(Root.getAbsolutePath() + "/MyApp");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(dir, "MyMessage.txt");
        try {
            fileOutputStream = new FileOutputStream(file, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    } else {
        Toast.makeText(getApplicationContext(), "SDcard not found", Toast.LENGTH_LONG).show();
    }

    OnStore = (ToggleButton) findViewById(R.id.onStore);
    OnStore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (OnStore.isChecked()){
                try {
                    for(TotalAccelerate : list){
                         String space = "\n";
                        byte[] convert = space.getBytes();
                        fileOutputStream.write(convert);
                        String finalData;
                        finalData = String.valueOf(TotalAccelerate);
                        fileOutputStream.write(finalData.getBytes());
                    }
                     Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }if (!OnStore.isChecked()){
                try {
                    fileOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fileOutputStream.close();
                     //added
                    list.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show();

            }


        }

    });
}// end OnCreate method

//Find the total acccerleration from the three sensors, 
  then write the values into a file for off-line analysis
@Override
public final void onSensorChanged(SensorEvent event) {
    // The light sensor returns a single value.
    // Many sensors return 3 values, one for each axis.
    double xx = event.values[0];
    double yy = event.values[1];
    double zz = event.values[2];
    TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2)
            + Math.pow(yy, 2)
            + Math.pow(zz, 2)));
    Log.i(DEBUG, "Accelerometer = " + TotalAccelerate);

    //  list = new ArrayList<Double>();
    list.add(TotalAccelerate);
    findPeaks(list);
    sensorText.setText("Total: " + list);
    Log.i(DEBUG, "Total " + list);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-25 13:56:08

每次传感器发生变化时,您都要创建一个新的列表,其行是:-

list = new ArrayList<Double>();

将其移动到程序的开始,并在将其写入文件后清空列表。您还需要同步修改列表的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41853128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档