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

  • Home
  • SEARCH
  • 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 593547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:51:26+00:00 2026-05-13T15:51:26+00:00

I have seen a few code examples of 1.0 / 1.1 but since a

  • 0

I have seen a few code examples of 1.0 / 1.1 but since a lot of that is deprecated now I was hoping to find a nice 1.5+ example. I have some code here that I wrote but it doesn’t really work properly. Any help would be great, thanks!

public class collectAccel extends Activity implements SensorEventListener, 
                                                OnClickListener{

private SensorManager sensorMgr;
private TextView xLabel, yLabel, zLabel;
private Button StartBtn;
private List<Sensor> sensorList;

private float x, y, z;
private long lastUpdate = -1;
// deltas for calibration
private float cx, cy, cz;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    xLabel = (TextView) findViewById(R.id.x_label);
    yLabel = (TextView) findViewById(R.id.y_label);
    zLabel = (TextView) findViewById(R.id.z_label);
    StartBtn = (Button) findViewById(R.id.Button1);
    StartBtn.setOnClickListener(this);
}


@Override
public void onClick(View arg0) {
    if (xLabel.getVisibility() != 1)
        xLabel.setVisibility(1);
    if (yLabel.getVisibility() != 1)
        yLabel.setVisibility(1);
    if (zLabel.getVisibility() != 1)
        zLabel.setVisibility(1);
}

@Override
protected void onPause() {
    super.onPause();

    sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    sensorMgr = null;

    cx = 0;
    cy = 0;
    cz = 0;
}

@Override
protected void onResume() {
    super.onResume();
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorList = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);

    boolean accelSupported = sensorMgr.registerListener((SensorEventListener)this, 
            sensorList.get(0),
            SENSOR_DELAY_UI);

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    }
}



@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    cx = 0;
    cy = 0;
    cz = 0;

}


@Override
public void onSensorChanged(SensorEvent arg0) {
    if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms, otherwise updates
        // come way too fast and the phone gets bogged down
        // with garbage collection
        if (lastUpdate == -1 || (curTime - lastUpdate) > 100) {
            lastUpdate = curTime;

            x = arg0.values[0];
            y = arg0.values[1];
            z = arg0.values[2];
            xLabel.setText(String.format("X: %+2.5f (%+2.5f)", (x+cx), cx));
            yLabel.setText(String.format("Y: %+2.5f (%+2.5f)", (y+cy), cy));
            zLabel.setText(String.format("Z: %+2.5f (%+2.5f)", (z+cz), cz));
        }
    }
}
}
  • 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-05-13T15:51:27+00:00Added an answer on May 13, 2026 at 3:51 pm

    You can look it up in the Android Reference but It looks like you’ll need

    SensorManager mgr = Context.getSystemService(SENSOR_SERVICE);
    IList<Sensor> sensorList = mgr.getSensorList(SensorManager.SENSOR_ACCELEROMETER);
    

    If you really want the list, but I think you are actually meant to use SensorManager.RegisterListener(SensorListener listener, int sensors, int rate) or another version of that method.

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

Sidebar

Related Questions

I've seen in a few iPhone examples that attributes have used an underscore _
I have seen quite a few code samples/plugins that promote uploading assets directly to
I've seen a few examples on here to this problem, but none have solved
I have seen a lot of examples, but I am not sure how it
I've seen a few similar posts to my problem, but none that have helped
I have seen a few sites now where if you highlight text of an
I have seen a few posts regarding this issue but not one specific to
I've seen a few sites that have a flash app which takes video from
I have seen a couple of instances now where some legacy code I'm working
I have seen a few examples lately where if statements are written as follows:

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.