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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:59:24+00:00 2026-05-28T03:59:24+00:00

I have used: public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener to implement touch

  • 0

I have used:

public class MainGamePanel extends SurfaceView 
    implements SurfaceHolder.Callback, SensorEventListener

to implement touch events and accelerometer events. Using @Override, I have overriden the touch events, and they are working correctly. I am not getting anything from the accelerometer events though, like onSensorChanged()

Is there an obvious reason why?

  • 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-28T03:59:24+00:00Added an answer on May 28, 2026 at 3:59 am

    You will need to register with the system’s SensorManager in order to receive updates from any sensors. A very basic example on how to do this is given in the documentation of SensorManager.

    The steps are:

    1) Get a reference to the system’s SensorManager as well as the sensor(s) you would like to listen for updates for. In your case that would at least be the accelerometer. You need to request the SensorManager from a context. Since your MainGamePanel extends SurfaceView, use getContext().

    SensorManager mSensorManager = (SensorManager) getContext().getSystemService(Activity.SENSOR_SERVICE);
    Sensor mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    

    2) Once you have acquired these, you’ll need to register your class to listen for the actual updates – this is where SensorEventListener comes into the picture. Your SurfaceView implements it, so you should be able to register it as follows:

    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    

    3) That is actually all you have to do in order to start receiving updates from hardware sensors. From here on, events should start hitting your overridden onSensorChanged() method. You should however be aware of the fact that it’s common practice to release system resources as soon as you don’t actively use/need them anymore. Hence, don’t forget to unregister for sensor updates. An appropriate place for this would be in an onPause() or onStop() call.

    mSensorManager.unregisterListener(this);
    

    Google says:

    Always make sure to disable sensors you don’t need, especially when
    your activity is paused. Failing to do so can drain the battery in
    just a few hours. Note that the system will not disable sensors
    automatically when the screen turns off.

    Hope that helps you. If not, browse a bit around, because there are numerous examples illustrating how to set up a listener for sensor events.

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

Sidebar

Related Questions

I'm used to the Java model where you can have one public class per
I have a SwingWorker as follows: public class MainWorker extends SwingWorker(Void, MyObject) { :
I have used a custom class to implement shared resource functionality in my WPF
I have used following code for download some files from our internet. public class
I have used IPC in Win32 code a while ago - critical sections, events,
I have problems creating a custom query. This is what entities: @Entity public class
Actually i have created an singleton class. Now my singleton class extends Activity, and
I have a web service which returns something of type MyData . public class
I have created an Asynchronous WebClient request inside a class as follows: public class
I have this ValueConverter: public class LabelConverter : IValueConverter { public object Convert(object value,

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.