user3303887
user3303887

Reputation: 25

How do I acquire accelerometer data on android

I currently have an android application which has an accelerometer, I want to collect the data and put it in a database, does anyone know how to do this? I am a beginner with android and I'm having some trouble with this particular aspect. Any help will be appreciated.

Upvotes: 1

Views: 3102

Answers (3)

Nana Ghartey
Nana Ghartey

Reputation: 7927

To add to Ziprox09's answer:

According to the android documentation http://developer.android.com/reference/android/hardware/SensorEvent.html

"The coordinate-system is defined relative to the screen of the phone in its default orientation. The axes are not swapped when the device's screen orientation changes."

Use the solution provided by NVidia to deal with the device default orientation issue: https://stackoverflow.com/a/14313663/1489493

Upvotes: 1

Zied R.
Zied R.

Reputation: 4964

1) AccelerometerActivity.java

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;

public class AccelerometerActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;

    TextView xCoor; // declare X axis object
    TextView yCoor; // declare Y axis object
    TextView zCoor; // declare Z axis object

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
        yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
        zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object

        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        // add listener. The listener will be  (this) class
        sensorManager.registerListener(this, 
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);


    }

    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }

    public void onSensorChanged(SensorEvent event){

        // check sensor type
        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

            // assign directions
            float x=event.values[0];
            float y=event.values[1];
            float z=event.values[2];

            xCoor.setText("X: "+x);
            yCoor.setText("Y: "+y);
            zCoor.setText("Z: "+z);
        }
    }
}

2. Layout: main.xml:

?xml version="1.0" encoding="utf-8"?>
<TableLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow>
    <TextView 
        android:id="@+id/xcoor"
        android:text="X Coordinate: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    </TableRow>

    <TableRow>
    <TextView 
        android:id="@+id/ycoor"
        android:text="Y Coordinate: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    </TableRow>

    <TableRow>
    <TextView 
        android:id="@+id/zcoor"
        android:text="Z Coordinate: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    </TableRow>

</TableLayout>

3)The Manifest(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.accelerometeractivity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="13" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

        <activity android:name=".AccelerometerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

4) THe result

enter image description here

Upvotes: 3

Piotr Ślesarew
Piotr Ślesarew

Reputation: 2836

If you do not know how to handle accelerometer or gyroscope in Android check Motion Sensors. You can also find a lot of samples in google.

Upvotes: 0

Related Questions