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 8371149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:05:31+00:00 2026-06-09T14:05:31+00:00

I tried to create seperate accelerometer class that holds values of accelerometer and I

  • 0

I tried to create seperate accelerometer class that holds values of accelerometer and I can acces them from any other class whenever I want, but it doesn’t seem to work.

Here is my Accelerometer class:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

public class Accelero implements SensorEventListener {

    private float xAxis;
    private float yAxis;
    private float zAxis;

    public float getX(){
        return this.xAxis;
    }

    public float getY(){
        return this.yAxis;
    }

    public float getZ(){
        return this.zAxis;
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    public void onSensorChanged(SensorEvent event) {
            xAxis = event.values[0];
            yAxis = event.values[1];
            zAxis = event.values[2];
    }

}

so as you see I am trying to get values through get*(); method that I created. But it seems always to return 0, 0, 0 . Any idea what is wrong with my code?

Here is the Activity that this is used in:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText output;
    private static int DIS = 1;
    Accelero acc;

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

        output =(EditText) findViewById(R.id.editText1);
        handler.sendEmptyMessageDelayed(DIS, 1);
        acc = new Accelero(this);

    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == DIS){
                refresh();
                handler.sendEmptyMessageDelayed(DIS, 1);     
            }
        }
    };

    public void refresh(){
        output.setText("X:"+acc.getX()+"\nY:"+acc.getY()+"\nZ:"+acc.getZ());
    }
}

I also have this premission in manifest:

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
  • 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-09T14:05:32+00:00Added an answer on June 9, 2026 at 2:05 pm

    You have to register the sensor that you are using, in this case the accelerometer.
    I’m guessing you want everything related to the accelerometer in the “Accelero” class.

    To register the listener from that class, you’ll have to pass a reference to your activity. So your constructor should look something like this:

    SensorManager manager;
    Sensor accelerometer;
    Activity foo;
    
    public Accelero(Activity foo) {
        this.foo = foo;
        manager = (SensorManager) this.foo.getSystemService(Context.SENSOR_SERVICE);
        accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
     }
    

    and when you create a Accelero object, you simply pass this as the parameter.

    I hope this helps.

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

Sidebar

Related Questions

I have tried create this table, but nothing I have tried works from FKs.
I tried to create jar file using command line that I inserted into java
I tried to create a code that adds 2 percent each time a component
I tried to create a single insert query for the values in template_list array.
I'm building an Xml-driven application. I create new states in a seperate actionscript-class. These
I want to create an image that displays all the names from a database.
I'm trying to create a branch spec that only pulls down specific folders from
Tried to create a mobile navigation using dataview or outline and the view renders
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I tried to create a script to list the contents of a directory: #!/bin/bash

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.