Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how is the sensor suppose to detect that there's no movement coming from the phone? Because the phone is still, the numbers keep running. Thanks for the help in advance.



package com.example.testsensors;

import java.util.List;
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;
import android.widget.Toast;

public class TestSensors extends Activity implements SensorEventListener {
    private boolean theRegisteredSensor;
    private SensorManager theSensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        theRegisteredSensor = false;
        theSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    }
        protected void onStart() {
                super.onResume();
                List<sensor> sensors = theSensorManager
                .getSensorList(Sensor.TYPE_ACCELEROMETER);
                if (sensors.size() > 0) {
                        Sensor sensor = sensors.get(0);
                        theRegisteredSensor = theSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
                }
        }



        @Override
        protected void onPause() {
                if (theRegisteredSensor) {
                        theSensorManager.unregisterListener(this);
                        theRegisteredSensor = false;
                }
                super.onPause();
        }


        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            StringBuffer StringBuff = new StringBuffer();
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
                StringBuff.append("Accelerometer\n");
                StringBuff.append("X-axis:").append(event.values[0]).append("\n");
                StringBuff.append("Y-axis:").append(event.values[1]).append("\n");
                TextView tv = (TextView) findViewById(R.id.accero_counters);
                tv.setText(StringBuff.toString());
            Toast.makeText(getBaseContext()
            , "Movement is detected.",Toast.LENGTH_SHORT).show();
        }
   }
}
</sensor>
Posted

You have to take into account acceleration due to gravity. This will always be measured in a device with at least three orthogonal accelerometers.

To account for this, you have to have some sort of calibration where you put the device in a known orientation and make note of the accelerometer readings. Then when using the device, you calculate the rotation and translation between the current sensor readings and the ones from the calibration to determine movement and orientation.
 
Share this answer
 
v4
Meaning? So what I should do if there's no movement detected? What should I use instead?
 
Share this answer
 
How do I go about doing this? What can I do then?
 
Share this answer
 
Comments
Sandeep Mewara 30-Nov-10 3:58am    
Not an answer. Use 'Add Comment' feature to respond to an answer.
Kythen 30-Nov-10 13:53pm    
Come up with a calibration procedure if there isn't already one done on Android phones (I don't have one myself). Then study up on linear algebra and matrix math.

Beyond that, it's entirely up to you to decide what to do with the movement information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900