2010-08-18 2 views

답변

5
+0

다음

package edu.uw.android.thorm.wayfinder; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class CSensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mCompass; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layoutsensor); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mCompass = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mTextView = (TextView) findViewById(R.id.tvSensor); } // The following method is required by the SensorEventListener interface; public void onAccuracyChanged(Sensor sensor, int accuracy) { } // The following method is required by the SensorEventListener interface; // Hook this event to process updates; public void onSensorChanged(SensorEvent event) { float azimuth = Math.round(event.values[0]); // The other values provided are: // float pitch = event.values[1]; // float roll = event.values[2]; mTextView.setText("Azimuth: " + Float.toString(azimuth)); } @Override protected void onPause() { // Unregister the listener on the onPause() event to preserve battery life; super.onPause(); mSensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mCompass, SensorManager.SENSOR_DELAY_NORMAL); } } 

관련 XML 파일입니다. 두 가지 질문 : R과 값을 어떻게 초기화합니까? 또한 알림을받을 수있는 방법이 있습니까, 아니면 전체 작업 모델이 사용 중지 되었습니까? –

+1

OK, 소스 기반의 좋은 예를 발견했습니다. 여기에 복사하지는 않겠지 만, 안드로이드 용 git 저장소를 살펴 본다면 개발/샘플/Compass/src/com/example/android/compass/CompassActivity.java의 끝 부분을 살펴보십시오. –

+1

알림/이벤트는 좋지 않습니다. 센서를 할 수 있습니다. 그래서 경찰은 부서지기 때문입니다. 모든 사소한 aspect 변경은 많은 이벤트를 유발할 것이고, 본질적으로 데이터로 UI 스레드를 질식시킬 것이다. – CodeFusionMobile

8

다음은 나침반 제목을 취득하고 텍스트 뷰에 표시 기본적인 예이기 때문에 사용하는 표준 API 호출입니다. SensorEventListener 인터페이스를 구현하면됩니다. 다음 코드 줄 (즉 "mSensorManager.registerListener (this, mCompass, SensorManager.SENSOR_DELAY_NORMAL);"(OnResume() 이벤트 참조)에서 상수를 변경하여 이벤트가 시스템에 전달되는 속도를 변경할 수 있습니다. 그러나이 설정은 시스템에 대한 제안 일뿐입니다. 또한이 예제는 onReuse() 및 onPause() 메서드를 사용하여 사용하지 않을 때 리스너를 등록 및 등록 취소하여 배터리 수명을 보존합니다. 희망이 도움이됩니다. 네,이 문서에서 발견

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvSensor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 
+3

OP가 언급 한 권장되지 않는 오리엔테이션 센서를 여전히 사용하고 있지 않습니까? – Tim

+0

예 - 더 이상 사용되지 않습니다. – Vaiden

관련 문제