기술/beginning games2011.05.24 18:59
download download
public class AccelerometerTest extends Activity implements SensorEventListener{

StringBuilder b = new StringBuilder();
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   textView = new TextView(this);
   setContentView(textView);
   
   SensorManager manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
   if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()==0){
    textView.setText("가속도센서가 설치 되지 않았습니다.");
   } else {
    Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    if(!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)){
    textView.setText("센서를 등록할 수 없습니다.");
    }
   }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
public void onSensorChanged(SensorEvent event) {
b.setLength(0);
b.append("x: ").append(event.values[0]);
b.append("y: ").append(event.values[1]);
b.append("z: ").append(event.values[2]);
textView.setText(b.toString());
}

}
저작자 표시
신고
download download download
Posted by [lying dragon] Ed Kim