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

Accelerometer Data Storing Code

Discussion in 'Introductions' started by carltianrayP, Dec 5, 2013.

  1. carltianrayP

    carltianrayP Lurker
    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_ACCELE ROMETER);

    //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

    Welcome to Android Forums carltianrayP. This is the intro section... I'm not one to be asking about your code. :) We do have an area for our developers. You will want to check it out. Here is a link: Android Development - Android Forums
    I hope you enjoy your time here and find someone to assist with your code.
     
  3. Digital Controller

    Digital Controller The Real Bass Creator

    Welcome to the forums!

    Looks like olbriar has pointed you in the right direction, if you have any other questions feel free to let us know.
     
Loading...

Share This Page

Loading...