java 内存文件 加速_java - 从加速计传感器写入文件 - android - 堆栈内存溢出

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

FileOutputStream fileOutputStream;

double TotalAccelerate; //I made it global variable

ArrayList list;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list = new ArrayList();

//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();

list.add(TotalAccelerate);

findPeaks(list);

sensorText.setText("Total: " + list);

Log.i(DEBUG, "Total " + list);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值