title: Collecting data from BroadcastReceiver to csv
body: <p>I'm trying to log battery information to a csv file, for now I'm just trying to log after each time the BroadcastReceiver receives the intent. Here's a snippet:</p>
  
  <pre><code>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", "-&gt; 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", "-&gt; 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) {}
          });
      }
  
  };
  </code></pre>
  
  <p>The problem: As seen by the following logcat log, after each iteration of onReceive(), the retrieved info is shown one more than before. Furthermore, the data on the csv doesn't update/can't be seen until after unplugging and plugging back in the USB.</p>
  
  <pre><code>D/OpenGLRenderer﹕ Enabling debug mode 0
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; 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﹕ -&gt; 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﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; 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﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; 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﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; uri=content://media/external/file/550
  I/External Storage﹕ Scanned/storage/emulated/0/BatteryApp:
  I/External Storage﹕ -&gt; uri=content://media/external/file/550
  </code></pre>
  
  <p>Any ideas on why this happens and a solution if available would be greatly appreciated.</p>