开发者

How to display sensor values

How to check whether there is sensor type like accerelerometer,magenetic compass in android emulator. Whether there is default sensor is present in android emulator or we need to connect any sensor simulator to android emulator.

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 outp开发者_开发技巧utZ;

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

         }

    } 
}

This code not displaying sensor values. Its displaying only design part which is drawn in main.xml. Give me solution to display sensor values.


I have a working application with very similar code to this. The only real difference is that I don't do "if (event.sensor.getType() == Sensor.TYPE_ALL)". I would suggest putting a log or debug breakpoint in onSensorChanged and see if it gets called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜