在我的应用程序中,我试图从Android设备传感器获取数据,然后尝试将它们存储在一个文件中。 我已经能够从传感器检索数据并在EditText中成功输出,但我无法从EditText连续获取数据。虽然循环在这里显然没有用处。并且线程会创建一个字符串“entry”的副本,但我需要它在外面,所以我试图使用一个可调用但不确定我是否正确。 建议做同样的事情。
这是我想要做的。
btn_start.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Callable callable = new Callable() {
@Override
public Object call() throws Exception {
while(!btn_stop.isPressed()) {
entry += acc_x.getText().toString() + "," +
acc_y.getText().toString() + "," +
acc_z.getText().toString() + "," +
gyro_x.getText().toString() + "," +
gyro_y.getText().toString() + "," +
gyro_z.getText().toString() + "," +
user_name.getText().toString() + "\n";
}
return entry;
}
};
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<String> stringFuture = executorService.submit(callable);
try {
entry = stringFuture.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
如果您需要更多参考,Here是完整的代码。
答案 0 :(得分:1)
如果你想从EditText连续获取数据。
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//save it to file here or what ever you want
}
@Override
public void afterTextChanged(Editable s) {}
});