Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:25:14+00:00 2026-06-05T15:25:14+00:00

I have looked at many other threads that show how to use accelerometer values,

  • 0

I have looked at many other threads that show how to use accelerometer values, and even go across multiple classes, however none of the methods that I have found work and no matter what I do, I cannot get the values to display in a class other than in the one they are defined in. I know I have a working accelerometer and that my AccelerometerReader class is getting the values correctly, because I have tested them out by printing them in that class. I have some code below, but I can’t find out why it isn’t working, although I believe that only the starting values are being passed into another class, and that they are not updating. Any help would be much appreciated!

AccelerometerReader Class

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class AccelerometerReader extends Activity implements SensorEventListener {

private SensorManager sensorManager;
private float ax;   // these are the acceleration in x, y and z axes
private float ay;
private float az;

public AccelerometerReader(float x, float y, float z) {
    this.ax = x;
    this.ay = y;
    this.az = z;
}

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

        sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
   }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
   }

 @Override
 public void onSensorChanged(SensorEvent event) {
     if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
           return;

     if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
           setValueX(event.values[0]);
           setValueY(event.values[1]);
           setValueZ(event.values[2]);

     }

 }


 public void setValueX(float x) {
     ax=x;
 }

 public float getValueX() {
    return ax;
 }

 public void setValueY(float y) {
     ay=y;
 }

 public float getValueY() {
     return ay;
 }

 public void setValueZ(float z) {
     az=z;
 }

 public float getValueZ() {
     return az;
 }
}

Accelerometer Class

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Accelerometer extends Activity {

private float ax, ay, az;
TextView tvX, tvY, tvZ;
AccelerometerReader acc = new AccelerometerReader(ax, ay, az);

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

    tvX = (TextView) findViewById(R.id.tvX);
    tvY = (TextView) findViewById(R.id.tvY);
    tvZ = (TextView) findViewById(R.id.tvZ);

    tvX.setText("" + ax);
    tvY.setText("" + ay);
    tvZ.setText("" + az);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:25:15+00:00Added an answer on June 5, 2026 at 3:25 pm

    You have a ton of problems in the code:

    1. You have one Activity that performs a function (i.e. Get the “ACCELEROMETER” values), however you want to perform this function while the user is looking at a different activity. This is generally bad and won’t work. You will instead need to create a Service.

    2. You are passing values from one class to another, when you pass those values they are just copies. So anything that is done to those values from the other class does not affect the original class.

      //What ever happens to ax, ay, and az will never be known to your class!
      AccelerometerReader acc = new AccelerometerReader(ax, ay, az);
      
    3. You do this once:

      tvX.setText("" + ax);
      tvY.setText("" + ay);
      tvZ.setText("" + az);
      

      even if ax, ay, and az changed the current code will only display them once. You have to recall that code every time the values change for them to be displayed.

    Well, good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked at many of the other questions but I think my problem
(I've looked at many other similar posts on SO and have unfortunately not been
I have looked through many tutorials, as well as other question here on stack
I have looked at the documentation on MSDN about these 2 functions. However, I
I have looked at other answers and the docs. Maybe I am missing something,
I have looked at other posts and can't see what I'm doing wrong. I'm
Let me start by saying I have looked at many similar questions asked, but
I have looked at other solutions to this on this site, but can't see
We have a fairly hefty VS2008 solution that comprises of many library projects that
I've been stuck at this for quite some time. I've looked at many other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.