java - 一次写入一个值

标签 java android android-layout listview android-studio

我正在尝试将 onSensorChanged( ) 方法的总加速度写入文件。我的处理写入文件的代码有问题。 我的文件只显示一个值。在我切换按钮停止写入之前,我想查看所有 值。您能否指出我的代码中哪里有问题。提前致谢。

 ToggleButton OnStore;   
 private GestureDetectorCompat mDetector;
FileOutputStream fileOutputStream;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     OnStore = (ToggleButton) findViewById(R.id.onStore);
    mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    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);

     OnStore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (OnStore.isChecked()){
                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);
                        String space =" ";
                        byte[] convert = space.getBytes();
                        fileOutputStream.write(convert);
                        String finalData;
                        finalData = String.valueOf(TotalAccelerate);
                        fileOutputStream.write(finalData.getBytes());
                        //fileOutputStream.close();
                        Toast.makeText(getApplicationContext(),"Message saving",Toast.LENGTH_LONG).show();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"SDcard not found",Toast.LENGTH_LONG).show();
                }
            }
            if (!OnStore.isChecked()){
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show();

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

        }
    });


  double TotalAccelerate;
  ArrayList<Double> list;

 @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,"Total "+ TotalAccelerate);
    list = new ArrayList<Double>();
    list.add(TotalAccelerate);

    //  findPeaks(list);
    sensorText.setText("Total: " + list);
    Log.i(DEBUG, "Total " + list);

}

最佳答案

你重新初始化你的 list每次在onSensorChanged(SensorEvent event)

你必须在 onCreate 中完成或在 if (OnStore.isChecked()){ 之后显示开始和停止之间的所有结果

更新

添加行 list = new ArrayList<Double>();onClick(View view)

     OnStore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (OnStore.isChecked()){
                list = new ArrayList<Double>();
                ...

并删除 list = new ArrayList<Double>();onSensorChanged(SensorEvent event)

更新 2

添加行 list = new ArrayList<Double>();onCreate(Bundle savedInstanceState)相反 onClick(View view)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    list = new ArrayList<Double>();
    ...

您所有的值都在 list

for(double TotalAccelerate : list){
    System.out.println(TotalAccelerate);
}

关于java - 一次写入一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41828886/

相关文章:

Android:在 collapsingToolbarLayout 的右上角放置一个图标?

android - 弹出软键盘时页面滚动

java - 在代码中获取 java.util.NoSuchElementException。为什么?

java - jsf中图片上传出现NullPointer异常

android - 我可以像使用 9 补丁一样使用矢量可绘制对象吗?

android - 如何在android中对日期和空日期进行排序?

安卓 : Orientation : Find out if a device supports orientations change

java - 当用户按下“提交”按钮时如何保存用户的评分和评论?

java - 如何刷新 Log4J2 中的异步记录器(使用中断器)

java - xml 布局错误 :The following classes could not be found: - android. support.v4.view.Viewpager