Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes. </p> <p>Using the <code>SensorEventListener.onSensorChanged(SensorEvent event)</code> you can determine the values provided along the X &amp; Y axis. You would need to record these values and then compare them to any new values that you receive on subsequent calls to the <code>onSensorChanged</code> method to get a delta value. If the delta value on one axis is positive then the device is moving one way, if its negative its moving the opposite way. </p> <p>You will probably need to fine tune both the rate at which you receive accelerometer events and the threshold at which you consider a delta value to indicate a change in direction.</p> <p>Here's a quick code example of what I'm talking about:</p> <pre class="lang-java prettyprint-override"><code>public class AccelerometerExample extends Activity implements SensorEventListener { TextView textView; StringBuilder builder = new StringBuilder(); float [] history = new float[2]; String [] direction = {"NONE","NONE"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); textView = new TextView(this); setContentView(textView); SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); } @Override public void onSensorChanged(SensorEvent event) { float xChange = history[0] - event.values[0]; float yChange = history[1] - event.values[1]; history[0] = event.values[0]; history[1] = event.values[1]; if (xChange &gt; 2){ direction[0] = "LEFT"; } else if (xChange &lt; -2){ direction[0] = "RIGHT"; } if (yChange &gt; 2){ direction[1] = "DOWN"; } else if (yChange &lt; -2){ direction[1] = "UP"; } builder.setLength(0); builder.append("x: "); builder.append(direction[0]); builder.append(" y: "); builder.append(direction[1]); textView.setText(builder.toString()); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // nothing to do here } } </code></pre> <p>This code will only provide you with the general direction on the X and Y axis that the phone has moved in. To provide a more fine grained determination of direction (e.g. to attempt to mimic the movement of a computer mouse) you might find that a phone's accelerometer is not fit for purpose. </p> <p>To attempt this, I would first set the sensor delay to <code>SensorManager.SENSOR_DELAY_FAST</code> and create a list of multiple history events so that I could detect movement over time and not be influenced by slight movements in the opposite direction that often happen when taking accelerometer measurements at such a fine level. You would also need to measure the amount of time that has passed to help calculate the accurate measure of movement as suggested in your comments. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload