83964

Sensor Activity in android

Question:

I am using the following class to read accelerometer data from an android mobile:

public class AccelerometerData extends Activity implements SensorEventListener { //variables public AccelerometerData() { mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { // TODO: my codes } } }

Now from my main activity class, I was calling that class to read the data

AccelerometerData sbt = new AccelerometerData ();

and it shows the following message <strong>Unfortunately AccelerometerData has stopped</strong>. I am new in android development and not sure what is wrong here.

Any help would be highly appreciated.

Answer1:

The answer you're looking for might look a little something like this. The MainActivity owns the AccelerometerData class.

public class MainActivity extends Activity { priavte AccelerometerData ad; private TextView someTextView; @Override protected void onCreate(Bundle savedInstanceState) { // "this" allows 'ad' to make calls back to the MainActivity ad = new AccelerometerData((SensorManager)getSystemService(SENSOR_SERVICE), this); someTextView = (TextView)findViewById(R.id.my_text_view); } protected void onResume() { super.onResume(); sensorHandler.onResume(); } protected void onPause() { super.onPause(); sensorHandler.onPause(); } protected void setTextViewValue(float[] values){ someTextView.setText( Float.toString(values[0]) ); }

Then in AccelerometerData class it might look something like this.

public class AccelerometerData implements SensorEventListener { private final SensorManager mSensorManager; private final Sensor mAccelerometer; private final MainActivity ma; public AccelerometerData(SensorManager sm, MainActivity ma){ mSensorManager = sm; mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); this.ma = ma } protected void onResume() { mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { // Here we call a method in MainActivity and pass it the values from the SensorChanged event ma.setTextViewValue( event.values ); }

}

Answer2:

Your class shouldn't extend Activity. It doesn't need to. It should be owned by an activity of some sort instead.

Recommend

  • Calling accelerometer from WallpaperService
  • How to set delay of Android Sensors programmatically?
  • Create service to detect any action from the user
  • How to use a typedef function pointer to register a callback
  • Android: Why DialogFragment return nullpointer on orientation change
  • Android listactivity scrolling with accelerometer issue
  • Sensor Activity in android
  • MySQL: Optimizing Searches with LIKE or FULLTEXT
  • Trouble opening utf-8 URI's with Ruby's 'open-uri'
  • Why is the 100*0.07 equal to 6.9999…? [duplicate]
  • Google directions api web service - show the response on map. Is it possible?
  • Ordering of eigenvectors when calculating eigenvectors using LAPACK's ssteqr
  • python function to set accuracy of float
  • How to decode route points from the JSON Output Data?
  • Get temperature from NVidia GPU using NVAPI
  • GDB skips over my code!
  • Is it possible to embed the sidebar with Google Maps?
  • ValueError: Found arrays with inconsistent numbers of samples
  • Autofac with Web API 2 - Parameter-less constructor error
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal