Android 强制ToggleButton停止写入本地内存文件
我有一个变量MyFinalPressure,其中填充了来自传感器压力的传感器数据。如果MyFinalPressure为Android 强制ToggleButton停止写入本地内存文件,android,performance,android-fragments,android-intent,Android,Performance,Android Fragments,Android Intent,我有一个变量MyFinalPressure,其中填充了来自传感器压力的传感器数据。如果MyFinalPressure为=至4000(4000点或40秒),则停止写入本地存储器。 但当我调试代码时,它似乎碰到了布尔值MaxPoints,并且仍在编写。我不知道我的逻辑是不是错了 你能帮帮我吗 public Boolean Store = false; Boolean MaxPoints = false; if (activity.Store) { activity.write
=
至4000(4000点或40秒),则停止写入本地存储器。
但当我调试代码时,它似乎碰到了布尔值MaxPoints,并且仍在编写。我不知道我的逻辑是不是错了
你能帮帮我吗
public Boolean Store = false;
Boolean MaxPoints = false;
if (activity.Store) {
activity.writeToFile(MyFinalPressure);//MyFinalPressure is float of one dimension array or stream of array.
}
if (MyFinalPressure==4000){ //this conditon, am trying to stop wrting to local memory.
activity.MaxPoints = true;
}
FileOutputStream fileOutputStream;
//method to write into local memory.
public void writeToFile(final float MyFinalPressure) {
Log.d(TAG, "writeToFile.");
String finalData;
finalData = String.valueOf(MyFinalPressure);
try {
// true here for 'append'
fileOutputStream = new FileOutputStream(file, true);
String Space = " ";
byte[] convert = Space.getBytes();
fileOutputStream.write(finalData.getBytes());
fileOutputStream.write(convert);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file.
StartWriting = (ToggleButton) findViewById(R.id.startWriting);
StartWriting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (StartWriting.isChecked()) {
Store = true;
Toast.makeText(getApplicationContext(), "Data Starts writing into (Message.txt) file", Toast.LENGTH_LONG).show();
} else {
if (!StartWriting.isChecked()|| MaxPoints==true) { //here - this is wrong logic to stop writing to my file.
Toast.makeText(getApplicationContext(), "Data Stored at myAppFile", Toast.LENGTH_SHORT).show();
String finalData1;
finalData1 = String.valueOf(fileOutputStream);
Log.i(TAG, "of_writes: " + finalData1);
// Toast.makeText(getApplicationContext(), "Data_write_number: " + finalData1.length(), Toast.LENGTH_SHORT).show();
Store = false;
}
}
}
});
拿出去!StartWriting.isChecked() 由于上面的if语句StartWriting.isChecked()将始终为false。然后,因为您正在检查“!StartWriting.isChecked()”,所以它将始终输入语句