一次写入一个值
Posted
技术标签:
【中文标题】一次写入一个值【英文标题】:Write one value at a time 【发布时间】:2017-06-09 07:02:06 【问题描述】:我正在尝试将 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);
【问题讨论】:
【参考方案1】:您每次在onSensorChanged(SensorEvent event)
中重新初始化您的list
您必须在onCreate
或if (OnStore.isChecked())
之后执行此操作才能显示开始和停止之间的所有结果
更新
在onClick(View view)
中添加行list = new ArrayList<Double>();
OnStore.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (OnStore.isChecked())
list = new ArrayList<Double>();
...
并删除onSensorChanged(SensorEvent event)
中的list = new ArrayList<Double>();
更新 2
在onCreate(Bundle savedInstanceState)
中添加行list = new ArrayList<Double>();
而不是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);
【讨论】:
我还是没明白。请你写一个代码来说明一下。 它可以工作,但是当我的 OnStore.isChecked 时,我需要拥有 **TotalAccelerate ** 的所有值。我怎样才能做到这一点。我觉得我需要一个线程。 我的意思是我希望所有值都存储在我的 .txt 文件中。我还是没听懂。以上是关于一次写入一个值的主要内容,如果未能解决你的问题,请参考以下文章