I downloaded and managed to make the sensor simulator works on the Android AVD simulator. I also used a simple accelerometer test code from the internet:
Using java Syntax Highlighting
- package org.example.AccelOrientExample;
- import org.example.AccelOrientExample.R;
- 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 AccelOrientExample extends Activity implements SensorEventListener {
- // Accelerometer X, Y, and Z values
- private TextView accelXValue;
- private TextView accelYValue;
- private TextView accelZValue;
- // Orientation X, Y, and Z values
- private TextView orientXValue;
- private TextView orientYValue;
- private TextView orientZValue;
- private SensorManager sensorManager = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Get a reference to a SensorManager
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- setContentView(R.layout.main);
- // Capture accelerometer related view elements
- accelXValue = (TextView) findViewById(R.id.accel_x_value);
- accelYValue = (TextView) findViewById(R.id.accel_y_value);
- accelZValue = (TextView) findViewById(R.id.accel_z_value);
- // Capture orientation related view elements
- orientXValue = (TextView) findViewById(R.id.orient_x_value);
- orientYValue = (TextView) findViewById(R.id.orient_y_value);
- orientZValue = (TextView) findViewById(R.id.orient_z_value);
- // Initialize accelerometer related view elements
- accelXValue.setText("0.00");
- accelYValue.setText("0.00");
- accelZValue.setText("0.00");
- // Initialize orientation related view elements
- orientXValue.setText("0.00");
- orientYValue.setText("0.00");
- orientZValue.setText("0.00");
- }
- // This method will update the UI on new sensor events
- public void onSensorChanged(SensorEvent sensorEvent) {
- synchronized (this) {
- if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
- accelXValue.setText(Float.toString(sensorEvent.values[0]));
- accelYValue.setText(Float.toString(sensorEvent.values[1]));
- accelZValue.setText(Float.toString(sensorEvent.values[2]));
- }
- if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
- orientXValue.setText(Float.toString(sensorEvent.values[0]));
- orientYValue.setText(Float.toString(sensorEvent.values[1]));
- orientZValue.setText(Float.toString(sensorEvent.values[2]));
- }
- }
- }
- // I've chosen to not implement this method
- public void onAccuracyChanged(Sensor arg0, int arg1) {
- // TODO Auto-generated method stub
- }
- @Override
- protected void onResume() {
- super.onResume();
- // Register this class as a listener for the accelerometer sensor
- sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
- // ...and the orientation sensor
- sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
- }
- @Override
- protected void onStop() {
- // Unregister the listener
- sensorManager.unregisterListener(this);
- super.onStop();
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:stretchColumns="1">
- <TableRow>
- <TextView
- android:id="@+id/accelerometer_label"
- android:layout_column="1"
- android:text="Accelerometer"
- android:textSize="9pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/accel_x_label"
- android:layout_column="1"
- android:text="X:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/accel_x_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/accel_y_label"
- android:layout_column="1"
- android:text="Y:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/accel_y_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/accel_z_label"
- android:layout_column="1"
- android:text="Z:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/accel_z_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#FF909090" />
- <TableRow>
- <TextView
- android:id="@+id/orientation_label"
- android:layout_column="1"
- android:text="Orientation"
- android:textSize="9pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/orient_x_label"
- android:layout_column="1"
- android:text="X:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/orient_x_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/orient_y_label"
- android:layout_column="1"
- android:text="Y:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/orient_y_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/orient_z_label"
- android:layout_column="1"
- android:text="Z:"
- android:textSize="8pt"
- android:padding="3dip" />
- <TextView
- android:id="@+id/orient_z_value"
- android:gravity="right"
- android:textSize="8pt"
- android:padding="3dip" />
- </TableRow>
- </TableLayout>
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
I was thinking if I run the sensor simulator first, then run the AccelOrientExample, then move the Visual Phone, the data on the AccelOrientExample would also change, but it doesn't. Only the values on the sensor simulator changes obviously. Did I do something wrong or the AccelOrientExample just doesn't work. Sorry because I don't have any Android phone to test atm so is there anybody can help me to test the code above on a real device. I really appreciate it.


