- public class GetPosition extends Activity {
- private TextView TextView01;
- private SensorManager mSensorManager;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.get_position);
- TextView01 = (TextView) findViewById(R.id.textView1);
- mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- }
- protected void onResume() {
- super.onResume();
-
- mSensorManager.registerListener(mSensorEventListener,
- mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
- SensorManager.SENSOR_DELAY_NORMAL);
- }
- protected void onPause() {
- mSensorManager.unregisterListener(mSensorEventListener);
- super.onPause();
- }
- private final SensorEventListener mSensorEventListener = new SensorEventListener() {
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- public void onSensorChanged(SensorEvent event) {
-
- if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
-
- float x_data = event.values[SensorManager.DATA_X];
- if ((x_data > 0 && x_data <= 22.5) || x_data > 337.5) {
- TextView01.setText("北方" + String.valueOf(x_data));
- } else if (x_data > 22.5 && x_data <= 67.5) {
- TextView01.setText("东北方" + String.valueOf(x_data));
- } else if (x_data > 67.5 && x_data <= 112.5) {
- TextView01.setText("东方" + String.valueOf(x_data));
- } else if (x_data > 112.5 && x_data <= 157.5) {
- TextView01.setText("东南方" + String.valueOf(x_data));
- } else if (x_data > 157.5 && x_data <= 202.5) {
- TextView01.setText("南方" + String.valueOf(x_data));
- } else if (x_data > 202.5 && x_data <= 247.5) {
- TextView01.setText("西南方" + String.valueOf(x_data));
- } else if (x_data > 247.5 && x_data <= 292.5) {
- TextView01.setText("西方" + String.valueOf(x_data));
- } else if (x_data > 292.5 && x_data <= 337.5) {
- TextView01.setText("西北方" + String.valueOf(x_data));
- }
- }
- }
- };
- }
public class GetPosition extends Activity { private TextView TextView01; private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.get_position); TextView01 = (TextView) findViewById(R.id.textView1); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } protected void onResume() { super.onResume(); /* 取得方守性的Sensor,并注册SensorEventListener */ mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { mSensorManager.unregisterListener(mSensorEventListener); super.onPause(); } private final SensorEventListener mSensorEventListener = new SensorEventListener() { public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { /* 判断Sensor的种类 */ if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { /* 取得X值资料 */ float x_data = event.values[SensorManager.DATA_X]; if ((x_data > 0 && x_data <= 22.5) || x_data > 337.5) { TextView01.setText("北方" + String.valueOf(x_data)); } else if (x_data > 22.5 && x_data <= 67.5) { TextView01.setText("东北方" + String.valueOf(x_data)); } else if (x_data > 67.5 && x_data <= 112.5) { TextView01.setText("东方" + String.valueOf(x_data)); } else if (x_data > 112.5 && x_data <= 157.5) { TextView01.setText("东南方" + String.valueOf(x_data)); } else if (x_data > 157.5 && x_data <= 202.5) { TextView01.setText("南方" + String.valueOf(x_data)); } else if (x_data > 202.5 && x_data <= 247.5) { TextView01.setText("西南方" + String.valueOf(x_data)); } else if (x_data > 247.5 && x_data <= 292.5) { TextView01.setText("西方" + String.valueOf(x_data)); } else if (x_data > 292.5 && x_data <= 337.5) { TextView01.setText("西北方" + String.valueOf(x_data)); } } } };}
阅读(1000) | 评论(0) | 转发(0) |