Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Androidで加速度センサーから情報を取得する
public class SensorActivity extends Activity implements SensorEventListener {
Private SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// センサーのインスタンス生成
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// リスナーから解除する
sensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// リスナーに加える
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(sensors.size() > 0) {
Sensor s = sensors.get(0);
sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_UI);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// 情報の取得
String str = event.values[SensorManager.DATA_X] + ", "
+ event.values[SensorManager.DATA_Y] + ", "
+ event.values[SensorManager.DATA_Z];
Log.d("TAG", str);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment