1. Are you ready for the Galaxy S20? Here is everything we know so far!

Accelerometer Data Storing Code

Discussion in 'Android Help' started by Android Question, Dec 5, 2013.

  1. Android Question

    Thread Starter

    i have downloaded this simple code and that i wanted the code to be modified so that it can store the accelerometer data to a .txt file or .csv file. i am very new to android programming and so i wanted to see how the coding first. Pls bear with my incapabilities. here is the code:

    package com.example;

    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

    public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;

    TextView title,tv,tv1,tv2;
    RelativeLayout layout;


    @Override
    public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //get layout
    layout = (RelativeLayout)findViewById(R.id.relative);

    //get textviews
    title=(TextView)findViewById(R.id.name);
    tv=(TextView)findViewById(R.id.xval);
    tv1=(TextView)findViewById(R.id.yval);
    tv2=(TextView)findViewById(R.id.zval);


    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy)
    {
    // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event)
    {
    // Many sensors return 3 values, one for each axis.
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];


    //display values using TextView
    title.setText(R.string.app_name);
    tv.setText("X axis:" +"\t\t"+x);
    tv1.setText("Y axis:" + "\t\t" +y);
    tv2.setText("Z axis:" +"\t\t" +z);

    }

    @Override
    protected void onResume()
    {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
    }
    }





    i will be very glad to have a positive response to this thread. :)
     



    1. Download the Forums for Android™ app!


      Download

       
  2. olbriar

    olbriar  
    Moderator

    I see that you joined Android Forums. That is great. I'm certain you will receive better assessment of your project there. Thanks for joining. :)
     
  3. carltianrayP

    carltianrayP Lurker

    can you help me with this project?
     
Loading...

Share This Page

Loading...