2012. 9. 9. 17:59

가끔 헤깔릴때가 있는데 센서값은 manifast 에 등록시켜주지 않아도 잘 작동한다. 


public class MainActivity extends Activity {

SensorManager mSm;

CompassView mView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mView = new CompassView();


mSm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

}


protected void onResume() {

super.onResume();

mSm.registerListener(mView, mSm.getDefaultSensor(Sensor.TYPE_ORIENTATION), 

SensorManager.SENSOR_DELAY_UI);

}


protected void onPause() {

super.onPause();

mSm.unregisterListener(mView);

}


class CompassView implements SensorEventListener {

float azimuth;

float pitch;

float roll;

final static int MAX = 30;

Paint textPnt = new Paint();

Bitmap compass;

int width;

int height;

int w10;

int h10;

int thick;

int length;




public void onAccuracyChanged(Sensor sensor, int accuracy) {

}


public void onSensorChanged(SensorEvent event) {

float[] v = event.values;

switch (event.sensor.getType()) {

case Sensor.TYPE_ORIENTATION:

if (azimuth != v[0] || pitch != v[1] || roll != v[2]) {

azimuth = v[0];

pitch = v[1];

roll = v[2];

Log.e("Test Acitivitiy","--------------------"+pitch);

}

break;

}

}

}

}

Posted by k1rha