Accelerator - onSensorChanged() method argument? Please help

Put your problem here if it does not fit any of the other categories.

Accelerator - onSensorChanged() method argument? Please help

Postby monarcharthas89 » Tue Jun 29, 2010 11:12 pm

hi, i am working on a small Accelerator app which has 3 buttons, start, reset and quit. At first I want x,y,z are 9.81,0,0. Then when I hit start, the app start to update the data from the sensor and reset to make it bake to 9.81,0,0. This is my code I adapted and edited from the internet:
Code: Select all
package org.example.Accelerometer1;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Accelerometer1 extends Activity implements OnClickListener, SensorEventListener {
    /** Called when the activity is first created. */
   private TextView accelXValue;
   private TextView accelYValue;
   private TextView accelZValue;
   private TextView XVal;
   private TextView YVal;
   private TextView ZVal;
   
   private SensorManager sensorManager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
       
        View StartButton = findViewById(R.id.Start_button);
        StartButton.setOnClickListener(this);
        View ResetButton = findViewById(R.id.Reset_button);
        ResetButton.setOnClickListener(this);
        View ExitButton = findViewById(R.id.Exit_button);
        ExitButton.setOnClickListener(this);
    }
   
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.Reset_button:
           try {
              Reset();
              break;
           } catch (InterruptedException e) {
              e.printStackTrace();
           }
        case R.id.Start_button:
           try {
              Start();
              break;
           } catch (InterruptedException e) {
              e.printStackTrace();
           }
        case R.id.Exit_button:
            onStop();
           finish();
            break;       
        }
    }
    public void Start() throws InterruptedException {
          
    }
   
    public void Reset() throws InterruptedException {
       onStop();
       
       accelXValue = (TextView) findViewById(R.id.accel_x_value);
        accelYValue = (TextView) findViewById(R.id.accel_y_value);
        accelZValue = (TextView) findViewById(R.id.accel_z_value);
       
        accelXValue.setText("9.81");
        accelYValue.setText("0.00");
        accelZValue.setText("0.00");
    }
    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);
    }
    @Override
    protected void onStop() {
       // Unregister the listener
       sensorManager.unregisterListener(this);
       super.onStop();
    }

   @Override
   public void onSensorChanged(SensorEvent sensorEvent) {
      // TODO Auto-generated method stub
      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]));       
            }
      }
   }
}
   


I am wondering in order to start, I should call the onSensorChanged() method but I don't know what kind of argument to put in or if is there a better way to do it, I would really appreciate it
monarcharthas89
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 17, 2010 10:44 pm

Top

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests