by sunil.android » Fri Aug 07, 2009 6:51 pm
Hi...
I took your code and try to run this but i got errors.... plz help me..
this is my code....
package sun.pack;
import android.hardware.Sensors;
public class AccelerometerReader
{
/** True when the Accelerometer-functionality is basically available. */
boolean accelerometerAvailable = false;
boolean isEnabled = false;
/**
* Sets up an AccelerometerReader. Checks if Accelerometer is available on
* this device and throws UnsupportedOperationException if not .
*
* @param doEnable :
* enables the devices Accelerometer
* initially (if sensor available)
* @throws UnsupportedOperationException
* if Accelerometer is not available on this device.
*/
public AccelerometerReader(boolean doEnable)
throws UnsupportedOperationException {
/* Check once here in the constructor if an
* Accelerometer is available on this device. */
for (String aSensor : Sensors.getSupportedSensors())
if (aSensor.equals(Sensors.SENSOR_ACCELEROMETER))
accelerometerAvailable = true;
if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");
if (doEnable)
setEnableAccelerometer(true);
}
/**
* En/Dis-able the Accelerometer.
*
* @param doEnable
* <code>true</code> for enable.<br>
* <code>false</code> for disable.
* @throws UnsupportedOperationException
*/
public void setEnableAccelerometer(boolean doEnable)
throws UnsupportedOperationException
{
if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");
/* If should be enabled and not already is: */
if (doEnable && !this.isEnabled) {
Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
this.isEnabled = true;
} else /* If should be disabled and not already is: */
if (!doEnable && this.isEnabled) {
Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
this.isEnabled = false;
}
}
/**
* Read out the values currently provided by the Accelerometer.
*
* @return the current Accelerometer-values.
* @throws UnsupportedOperationException
* if Accelerometer is not available on this device.
* @throws IllegalStateException
* if Accelerometer was set to disabled.
*/
public float[] readAccelerometer() throws UnsupportedOperationException, IllegalStateException {
if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");
if (!this.isEnabled)
throw new IllegalStateException(
"Accelerometer was set to disabled!");
/* Get number of sensor-values the sensor will return. Could be
* variable, depending of the amount of axis (1D, 2D or 3D
* accelerometer). */
int sensorValues = Sensors
.getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
float[] out = new float[sensorValues];
/* Make the OS fill the array we passed. */
Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, out);
/* And return it. */
return out;
}
}
//--------------------------------------------------------------------------------------------------
package sun.sensors;
import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
@SuppressWarnings("deprecation")
public class SensorsDemoActivity extends Activity implements SensorListener
{
final String TAG = "SensorDemoActivity";
SensorManager sm =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
}
@Override
public void onAccuracyChanged(int sensor, int accuracy)
{
// TODO Auto-generated method stub
Log.d(TAG,"onAccuracyChanged:()--" + sensor + ", accuracy: " + accuracy);
}
@Override
public void onSensorChanged(int sensor, float[] values)
{
// TODO Auto-generated method stub
if(sensor == SensorManager.SENSOR_ACCELEROMETER)
{
Log.v(TAG, "x-->"+values[0]);
Log.v(TAG, "y-->"+values[1]);
Log.v(TAG, "z-->"+values[2]);
}
}
}
Thanks & Regards
Sun