2015-04-15 26 views
1

我想記錄電池信息到一個CSV文件,現在我只是試圖在每次BroadcastReceiver收到意圖後記錄。這裏的一個片段:從BroadcastReceiver收集數據到CSV

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 

任何想法,爲什麼發生這種情況和解決方案,如果可用將不勝感激。

回答

1

從代碼中可以清楚地看到它。該消息正在您的TextWatcher偵聽器中記錄,並且每次調用onReceive方法時,都會連接另一個偵聽器。每次收到廣播時,不應重新創建偵聽器,而應僅創建一次。

此外,似乎直接生成響應廣播的日誌消息會更簡單很多,而不是更新UI元素,然後觸發另一個響應該事件的事件。

+0

我不知道我怎麼沒有發現......我現在只是測試日誌記錄以查看它是否有效,但我計劃將TextWatcher偵聽器實現爲一種不同的方法,以便應該解決事情,謝謝。 – Andrew