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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:33:38+00:00 2026-06-17T22:33:38+00:00

I have been working on logging data from accelerometer. Nonetheless, I find it difficult

  • 0

I have been working on logging data from accelerometer. Nonetheless, I find it difficult to control the on/off of the accelerometer sensor. I have tried to have two buttons, one called Start and the other Stop. I initialized my senor object in the onCreate() callback of the Activity Class. Then in the onClick() callback for the buttons, I checked the id of the View, if it’s Start button, I registerListen for the sensor, if it’s stop, I unregister it. This does not work as expected. And onSensorChanged(), I simply display the x,y, z accelerations. The sensor does what it needs to do no matter what buttons I clicked on, and even without clicking on any of the buttons. It seems to be that once the sensor is initialized, it keeps reading and there is no way to stop it. Is it possible to control the on/off of the sensor using two buttons?

Thanks

Update:

package com.example.firstapp;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener,OnClickListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;

private SensorManager mSensorManager;

private Sensor mAccelerometer;

private final float NOISE = (float) 2.0;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.start);
Button stopButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.y_axis);
TextView tvZ= (TextView)findViewById(R.id.z_axis);
ImageView iv = (ImageView)findViewById(R.id.image);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
iv.setVisibility(View.VISIBLE);
//if (deltaX > deltaY) {
//iv.setImageResource(R.drawable.horizontal);
//} else if (deltaY > deltaX) {
//iv.setImageResource(R.drawable.vertical);
//} else {
//iv.setVisibility(View.INVISIBLE);
//}
}
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
        case R.id.start:
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            break;
        case R.id.stop:
            mSensorManager.unregisterListener(this,mAccelerometer);
            break;
    }

}
}
  • 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-17T22:33:40+00:00Added an answer on June 17, 2026 at 10:33 pm

    just update the unregisterListener.

      case R.id.stop:
        //          mSensorManager.unregisterListener(this,mAccelerometer);
                    mSensorManager.unregisterListener(this);
                    break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a project on and off, but I haven't touched
I have been working on converting my websites session information over from flatfiles to
I am working on a data warehousing project, and therefore, I have been implementing
Have been working on this question for a couple hours and have come close
I have been working with SQL Server as a Developer a while. One thing
I have been working on a large java application. It is quite parallel, and
I have been working on this app for at least 3-4 months and just
I have been working on a project in C# (.net4). Project pretty much allows
I have been working with Zend for a few months now and am at
I have been working on a huge ETL project with 150+ tables and during

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.