2011-07-01 1 views
0

アンドロイドエミュレータにセンサーレス、マジェンティカルコンパスがあるかどうかをチェックする方法。 アンドロイドエミュレータにデフォルトのセンサーがあるかどうか、またはいずれかのセンサーシミュレータをアンドロイドエミュレータに接続する必要があります。センサ値を表示するには

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.LinearLayout; 
//import android.util.Log; 

import android.widget.TextView; 

public class SujaproActivity extends Activity implements SensorEventListener { 
SensorManager sensorManager ; 
private Sensor accSensor; 


private TextView outputX; 
private TextView outputY; 
private TextView outputZ; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    accSensor = sensorManager.getSensorList(
      Sensor.TYPE_ACCELEROMETER).get(0); 

    outputX = (TextView) findViewById(R.id.textView1); 





    outputY = (TextView) findViewById(R.id.textView2); 
    outputZ = (TextView) findViewById(R.id.textView3); 
    setContentView(R.layout.main); 

} 
protected void onResume() { 
    super.onResume(); 
    sensorManager.registerListener(this,accSensor,SensorManager.SENSOR_DELAY_GAME); 
    // sensorManager.registerListener(this, 
     /*sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
     SensorManager.SENSOR_DELAY_GAME);*/ 
} 
protected void onStop() { 
    super.onStop(); 
    sensorManager.unregisterListener(this); 
    /*sensorManager.unregisterListener(this, 
     sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));*/ 
    } 




public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // TODO Auto-generated method stub 

} 

public void onSensorChanged(SensorEvent event) { 




if (event.sensor.getType() == Sensor.TYPE_ALL) 

{   
       outputX.setText("x:"+Float.toString(event.values[0]));    
       outputY.setText("y:"+Float.toString(event.values[1])); 
       outputZ.setText("z:"+Float.toString(event.values[2])); 



       // default: 
       /*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;*/ 

     } 

    } 
} 

このコードはセンサー値を表示しません。 main.xmlに描画されたデザイン部分のみを表示します。センサの値を表示するための解決策を教えてください。

答えて

0

私はこれに非常に似たコードを使って動作するアプリケーションを持っています。唯一の違いは、if(event.sensor.getType()== Sensor.TYPE_ALL)を実行しないことです。私は、onSensorChangedにログまたはデバッグブレークポイントを設定し、呼び出されるかどうかを確認することをお勧めします。

関連する問題