2014-02-14 72 views
0

我目前爲我的應用程序創建了一個加速度計,我想創建兩個按鈕,在這裏我可以啓動和停止加速度計以收集數據。此時加速度計不斷運行。我想知道是否必須爲這些按鈕創建單獨的onclick方法?因爲我現在不能在我的手機/月食上工作。任何幫助將不勝感激。我的按鈕代碼是在onResume()方法和onPause()方法,是否正確?我是否需要爲傳感器創建單獨的按鈕方法?

import android.app.Activity; 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class movement extends Activity implements SensorEventListener { 

    private float mLastX, mLastY, mLastZ; 
    private boolean mInitialized; 
    private SensorManager mSensorManager; 
    private Sensor mAccelerometer; 
    private final float NOISE = (float) 2.0; 
    Button startButton, stopButton; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.movement); 
     mInitialized = false; 
     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_NORMAL); 

    } 

    protected void onResume() { 
     super.onResume(); 
     startButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mSensorManager.registerListener(movement.this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 

      } 
     }); 


    } 

    protected void onPause() { 
     super.onPause(); 
     stopButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mSensorManager.unregisterListener(movement.this); 
      } 
     }); 


    } 


    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // can be safely ignored for this demo 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     TextView tvX= (TextView)findViewById(R.id.x_axis); 
     TextView tvY= (TextView)findViewById(R.id.y_axis); 
     TextView tvZ= (TextView)findViewById(R.id.z_axis); 
     ImageView iv = (ImageView)findViewById(R.id.image); 
     float x = event.values[0]; 
     float y = event.values[1]; 
     float z = event.values[2]; 
     if (!mInitialized) { 
      mLastX = x; 
      mLastY = y; 
      mLastZ = z; 
      tvX.setText("0.0"); 
      tvY.setText("0.0"); 
      tvZ.setText("0.0"); 
      mInitialized = true; 
     } else { 
      float deltaX = Math.abs(mLastX - x); 
      float deltaY = Math.abs(mLastY - y); 
      float deltaZ = Math.abs(mLastZ - z); 
      if (deltaX < NOISE) deltaX = (float)0.0; 
      if (deltaY < NOISE) deltaY = (float)0.0; 
      if (deltaZ < NOISE) deltaZ = (float)0.0; 
      mLastX = x; 
      mLastY = y; 
      mLastZ = z; 
      tvX.setText(Float.toString(deltaX)); 
      tvY.setText(Float.toString(deltaY)); 
      tvZ.setText(Float.toString(deltaZ)); 
      iv.setVisibility(View.VISIBLE); 
      if (deltaX > deltaY) { 
       iv.setImageResource(R.drawable.horizontal); 
      } else if (deltaY > deltaX) { 
       iv.setImageResource(R.drawable.vertical); 
      } else { 
       iv.setVisibility(View.INVISIBLE); 
      } 
     } 
    } 
} 

回答

0

不,這是不完全正確的。您應該在您的活動的onCreate()方法中添加Clicklisteners,因爲onResume()僅在活動再次到達前面時調用,而onPause()僅在移動到後臺時調用。

有關活動的生命週期的詳細信息,請參閱從谷歌文檔的這一部分: http://developer.android.com/guide/components/activities.html

這裏我指的圖片:
http://developer.android.com/images/activity_lifecycle.png

此外,您還可以使用的onClick標籤在您的佈局XML文件中添加方法調用按鈕。您通常不必在預定義的按鈕上實現自己的監聽。

0

那麼,你需要一個行動阻止傳感器,所以stopButton需要自己的聽衆,是很容易

startButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mSensorManager.registerListener(movement.this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 

      } 
     }); 
stopButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // your code 
      } 
     }); 

尊重,在onResume創建的聽衆,更好地創造onCreate,因爲聽衆只會在activity lifecycle中創建一次,除此之外,如果您在onResume中創建,則它們將創建更多次。假設您創建活動(稱爲onResume),導航到其他活動並返回活動(onResume再次被調用)

相關問題