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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:34:10+00:00 2026-06-14T22:34:10+00:00

Problem Description: My friend and I are teaching ourselves android development. We debugged the

  • 0

Problem Description:

My friend and I are teaching ourselves android development. We debugged the other classes, like game physics and manually updated values, which drew the marble going diagonal across our board. When we changed it back to read in accelerometer data, the marble quit moving. Hence, we are going to include the class where we believe the problem is occurring. Basically, our view class in android development.

We are teaching ourselves android development and we can’t understand why data from the accelerometer, which we call mSensorX and mSensorY is not being updated. What we include below is our view class. Please note that we are developing for android 3.1. However, we are using all the latest goodies from android in eclipse.

package com.example.marblez;


import com.example.marblez.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

public class MarblezView extends View implements SensorEventListener{


//Sensor Stuff
private SensorManager mSensorManager;
private Display mDisplay;


//Accelerometer sensor stuff 
 private Sensor mAccelerometer;
 private float mSensorX;
 private float mSensorY;

 //Variables related to time 
 private long mCpuTimeStamp;
 private long mSensorTimeStamp;
 private WindowManager mWindowManager;



//Accelerometer buffer, such that slight movement will roll the marble 
private float sensor_buffer = 0;

//Create the canvas 
private Canvas mCanvas; 
private MarblezBackground background;

//Create the marble
private Marblez ball;

//Constructor for Marblez View 
@SuppressWarnings("deprecation")
public MarblezView(Context context, Activity activity){
    super(context);

    //Setup sensor stuff
    mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    mDisplay = mWindowManager.getDefaultDisplay();

    //Set up Maze 
    background = new MarblezBackground(activity);

    //Create our rolling little friend :-) 
    ball = new Marblez(activity);

}

 @Override
 public void onSensorChanged(SensorEvent event) {
     if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
         return;
     /*
      * record the accelerometer data, the event's timestamp as well as
      * the current time. The latter is needed so we can calculate the
      * "present" time during rendering. In this application, we need to
      * take into account how the screen is rotated with respect to the
      * sensors (which always return data in a coordinate space aligned
      * to with the screen in its native orientation).
      */

     switch (mDisplay.getRotation()) {
         case Surface.ROTATION_0:
             mSensorX = event.values[0];
             mSensorY = event.values[1];
             break;
         case Surface.ROTATION_90:
             mSensorX = -event.values[1];
             mSensorY = event.values[0];
             break;
         case Surface.ROTATION_180:
             mSensorX = -event.values[0];
             mSensorY = -event.values[1];
             break;
         case Surface.ROTATION_270:
             mSensorX = event.values[1];
             mSensorY = -event.values[0];
             break;
     }

     mSensorTimeStamp = event.timestamp;
     mCpuTimeStamp = System.nanoTime();
 }


//Automatic call
@Override
public void onDraw(Canvas canvas){
    mCanvas = canvas;

    //Draw the maze
    background.drawMaze(canvas);

    //Get the x and y sensor data and other goodies
    final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);

    //Draw the marble 
    ball.drawMarble(canvas, mSensorX, mSensorY, now);

    //Invalidate so it draws again 
    invalidate();

}

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

}

We got a lot of our ideas and methods from an open source google project called accelerometer play. However, those familiar with that code can see we went at lengths to try to simplify naming conventions, methods, and calls.

Basically, we would appreciate your help. It’s like the sensor is “there”, but for practical purposes it is never used. For example, it is very telling that view never calls the method onSensorChanged when we added a debug marker to it. Hence, another case in that our accelerometer really isn’t working.

  • 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-14T22:34:11+00:00Added an answer on June 14, 2026 at 10:34 pm

    you need to register your instance at the sensor manager like this:

    mSensorManager.registerListener(this, mAccelerometer,
                                    SensorManager.SENSOR_DELAY_NORMAL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem Description: Multi-player Game that works over the some kind of network, in game
Problem description I want to refactor a parser for a flexible csv like format
Problem Description I am writing application for Android and use native code, And test
Problem Description I'm writing module for Android (JAVA) that use native code (C) .
Problem description: Read an xml file, traverse to a particular node (element), if it
Problem description : - Step 1: Take input FILE_NAME from user at main thread.
Short problem description: XCode 4.2 install right Target on device, but debug (run) always
The following is the problem description: let c[n] be the catalan number for n
This is my homework, but please read my problem description first. I have to
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without

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.