package com.ShakeT1;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class ShakeT1 extends Activity implements SensorEventListener{
SensorManager sensorManager = null;
Sensor accSensor;
SensorEventListener acc;
TextView outputX;
TextView outputY;
TextView outputZ;
//for orientation values
TextView outputX2;
TextView outputY2;
TextView outputZ2;
Sensor ACCELEROMETER;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputX = (TextView) findViewById(R.id.TextView01);
outputY = (TextView) findViewById(R.id.TextView02);
outputZ = (TextView) findViewById(R.id.TextView03);
outputX2 = (TextView) findViewById(R.id.TextView04);
outputY2 = (TextView) findViewById(R.id.TextView05);
outputZ2 = (TextView) findViewById(R.id.TextView06);
ACCELEROMETER = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public void onResume() {
super.onResume();
boolean A =sensorManager.registerListener(this,ACCELEROMETER ,SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
super.onStop();
sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
synchronized (this) {
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
outputX.setText("x:"+Float.toString(event.values[0]));
outputY.setText("y:"+Float.toString(event.values[1]));
outputZ.setText("z:"+Float.toString(event.values[2]));
break;
case Sensor.TYPE_ORIENTATION:
outputX2.setText("x:"+Float.toString(event.values[0]));
outputY2.setText("y:"+Float.toString(event.values[1]));
outputZ2.setText("z:"+Float.toString(event.values[2]));
break;
}
}
}
}
/////////////////////////XML///////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView05"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView06"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
<TextView
android:id="@+id/TextView07"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="x:"
/>
</LinearLayout>
인터넷에 돌아다니는 소스를 이용해서 간단하게 텍스트 필드에 방향값을 나타내고 싶은데 이상하게 알수없는 이유로 어플리케이션이 꺼졌다는 메시지가 나오네요 ㅠㅠ
고수님들 어느 부분에서 잘못됬는지 알려주세요 ㅠ ..
참고로 단말기에서 돌려봤는데 잘 안되서요 ㅠㅠ




sensorManager가 null 이네요.
...
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
ACCELEROMETER = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
...