import android.app.*;
import android.content.*;
import android.hardware.*;
import android.os.*;
import android.widget.*;
public class AccelorometerTest extends Activity implements SensorEventListener
{
TextView textView;
StringBuilder builder = new StringBuilder();
@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 onSensorChanged(SensorEvent event)
{
builder.setLength(0);
builder.append("x: ");
builder.append(event.values[0]);
builder.append(", y: ");
builder.append(event.values[1]);
builder.append(", z: ");
builder.append(event.values[2]);
textView.setText(builder.toString());
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}
import android.content.*;
import android.hardware.*;
import android.os.*;
import android.widget.*;
public class AccelorometerTest extends Activity implements SensorEventListener
{
TextView textView;
StringBuilder builder = new StringBuilder();
@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 onSensorChanged(SensorEvent event)
{
builder.setLength(0);
builder.append("x: ");
builder.append(event.values[0]);
builder.append(", y: ");
builder.append(event.values[1]);
builder.append(", z: ");
builder.append(event.values[2]);
textView.setText(builder.toString());
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}
'코드조각모음' 카테고리의 다른 글
[android007] SD카드 예제 (0) | 2011.11.06 |
---|---|
[android006] 어셋(Asset) 예제 (0) | 2011.11.06 |
[android004] 키입력 예제 (0) | 2011.11.06 |
[android003] 멀티 터치 예제 (0) | 2011.11.06 |
[android002] 싱글 터치 예제 (0) | 2011.11.06 |