gpt4 book ai didi

java - 从 BroadcastReceiver 收集数据到 csv

转载 作者:行者123 更新时间:2023-11-30 02:14:06 26 4
gpt4 key购买 nike

我正在尝试将电池信息记录到一个 csv 文件中,现在我只是尝试在 BroadcastReceiver 每次收到 Intent 后进行记录。这是一个 fragment :

public class MainActivity extends Activity
private static final String TAG = "MainActivity";
double voltage;
float temperature;
int soc;
Float current;

CSVWriter writer = null;
File dir = new File(Environment.getExternalStorageDirectory() + "/BatteryApp");
FileWriter fw;
File f;
boolean fexists;

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.w(TAG, "App created");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

if (!dir.exists())
{
dir.mkdirs();
}

int a = 1;
fexists = true;
while(fexists) {
File newFilename = new File(dir + "/battery" + Integer.toString(a) + ".csv");
if (newFilename.exists()) {
a++;
} else {
f = newFilename;

MediaScannerConnection.scanFile(MainActivity.this, new String[]{f.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("External Storage", "Scanned" + dir + ":");
Log.i("External Storage", "-> uri=" + uri);
}
});
try {
writer = new CSVWriter(new FileWriter(f, true), ',');
String[] entries = "Voltage(V),Temperature(C),SOC(%),Current(Ah),".split(",");
writer.writeNext(entries);
writer.flush();
writer.close();

}
catch (IOException ioex) {
ioex.printStackTrace();
}
fexists = false;
}
}
text = (TextView) findViewById(R.id.textView)
}

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w(TAG, "Broadcast Receiver received");
//After receiving battery info from BatteryManager

text.setText(
voltage + " V\n"
+ temperature + " C\n"
+ current + " Ah\n"
+ soc + " %\n");
text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v(TAG, "Voltage(V): " + voltageText + " Temp(C): " + temperatureText + " SOC(%): " + socText + " Current(A): " + currentText );

try
{
writer = new CSVWriter(new FileWriter(f, true), ',');
String [] data = (voltageText + "," + temperatureText + "," + socText + "," + currentText + ",").split(",");
writer.writeNext(data);
writer.flush();
writer.close();
MediaScannerConnection.scanFile(MainActivity.this, new String[]{f.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("External Storage", "Scanned" + dir + ":");
Log.i("External Storage", "-> uri=" + uri);
}
});
}
catch(IOException ioex)
{
ioex.printStackTrace();
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void afterTextChanged(Editable s) {}
});
}

};

问题:正如下面的 logcat 日志所示,每次 onReceive() 迭代后,检索到的信息都会比之前多显示一个。此外,csv 上的数据只有在拔下并重新插入 USB 后才会更新/无法看到。

D/OpenGLRenderer﹕ Enabling debug mode 0
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
W/MainActivity﹕ Broadcast Receiver received
V/MainActivity﹕ Voltage(V): 4.236 Temp(C): 33.0 SOC(%): 84 Current(A): 0.048
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
W/MainActivity﹕ Broadcast Receiver received
V/MainActivity﹕ Voltage(V): 4.248 Temp(C): 33.0 SOC(%): 84 Current(A): 0.054
V/MainActivity﹕ Voltage(V): 4.248 Temp(C): 33.0 SOC(%): 84 Current(A): 0.054
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
W/MainActivity﹕ Broadcast Receiver received
V/MainActivity﹕ Voltage(V): 4.249 Temp(C): 33.0 SOC(%): 84 Current(A): 0.08
V/MainActivity﹕ Voltage(V): 4.249 Temp(C): 33.0 SOC(%): 84 Current(A): 0.08
V/MainActivity﹕ Voltage(V): 4.249 Temp(C): 33.0 SOC(%): 84 Current(A): 0.08
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
W/MainActivity﹕ Broadcast Receiver received
V/MainActivity﹕ Voltage(V): 4.247 Temp(C): 33.0 SOC(%): 85 Current(A): 0.029
V/MainActivity﹕ Voltage(V): 4.247 Temp(C): 33.0 SOC(%): 85 Current(A): 0.029
V/MainActivity﹕ Voltage(V): 4.247 Temp(C): 33.0 SOC(%): 85 Current(A): 0.029
V/MainActivity﹕ Voltage(V): 4.247 Temp(C): 33.0 SOC(%): 85 Current(A): 0.029
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550
I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
I/External Storage﹕ -> uri=content://media/external/file/550

如有任何关于为什么会发生这种情况的想法以及可用的解决方案,我们将不胜感激。

最佳答案

从您的代码看来很清楚。该消息被记录在您的 TextWatcher 监听器中,并且每次调用 onReceive 方法时,您都会附加另一个监听器。不要在每次接收到广播时都重新创建监听器,而应该只创建一次。

此外,直接响应广播生成日志消息似乎要简单得多,而不是更新 UI 元素然后触发另一个事件来响应它。

关于java - 从 BroadcastReceiver 收集数据到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29657751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com