2017-01-24 66 views
1

我試圖將onSensorChanged()方法的總加速度寫入文件。處理寫入文件的代碼有問題。 我的文件只顯示一個值。我想看到全部的值,直到我切換我的按鈕停止寫入。你能否指出我的代碼中哪裏是錯的?提前致謝。一次寫入一個值

ToggleButton OnStore; 
private GestureDetectorCompat mDetector; 
FileOutputStream fileOutputStream; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    OnStore = (ToggleButton) findViewById(R.id.onStore); 
    mDetector = new GestureDetectorCompat(this, new MyGestureListener()); 
    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); 

    OnStore.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (OnStore.isChecked()){ 
       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); 
         String space =" "; 
         byte[] convert = space.getBytes(); 
         fileOutputStream.write(convert); 
         String finalData; 
         finalData = String.valueOf(TotalAccelerate); 
         fileOutputStream.write(finalData.getBytes()); 
         //fileOutputStream.close(); 
         Toast.makeText(getApplicationContext(),"Message saving",Toast.LENGTH_LONG).show(); 

        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       }else { 
        Toast.makeText(getApplicationContext(),"SDcard not found",Toast.LENGTH_LONG).show(); 
       } 
      } 
      if (!OnStore.isChecked()){ 
       try { 
        fileOutputStream.flush(); 
        fileOutputStream.close(); 
        Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show(); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

     } 
    }); 


    double TotalAccelerate; 
    ArrayList<Double> list; 

@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,"Total "+ TotalAccelerate); 
    list = new ArrayList<Double>(); 
    list.add(TotalAccelerate); 

    // findPeaks(list); 
    sensorText.setText("Total: " + list); 
    Log.i(DEBUG, "Total " + list); 

} 

回答

1

您重新初始化listonSensorChanged(SensorEvent event)

每次你都必須做在onCreateif (OnStore.isChecked()){後,顯示開始之間的所有結果,並停止

UPDATE

附加線list = new ArrayList<Double>();onClick(View view)

 OnStore.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (OnStore.isChecked()){ 
       list = new ArrayList<Double>(); 
       ... 

,並刪除list = new ArrayList<Double>();onSensorChanged(SensorEvent event)

更新2

外接線list = new ArrayList<Double>();onCreate(Bundle savedInstanceState)代替onClick(View view)

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    list = new ArrayList<Double>(); 
    ... 

所有值都在list

for(double TotalAccelerate : list){ 
    System.out.println(TotalAccelerate); 
} 
+0

我還是沒有明白。你能否請你寫一個插圖的代碼。 –

+0

它可以工作,但是當我的OnStore.isChecked時,我需要具有** TotalAccelerate **的所有值。我怎樣才能做到這一點。我覺得我需要一個線程。 –

+0

我的意思是我想要存儲在我的.txt文件中的所有值。我仍然沒有得到它。 –