Calling accelerometer from WallpaperService


I got this Accelerometer class that holds values of accelerometer and I can acces them from any other class whenever I want. Normally I would create new object Accelerometer accelerometer = new Accelerometer(this); but when I am inside WallpaperService it doesn't let me use this as parameter.

Here is the Acclerometer class:

import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class Accelero implements SensorEventListener { private float xAxis; private float yAxis; private float zAxis; SensorManager manager; Sensor accelerometer; Activity activity; public Accelero(Activity activity) { this.activity = activity; manager = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE); accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); } 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]; } }

for example I tried accessing it from the sample code that came with SDK, the CubeWallpaper

import com.example.android.livecubes.R; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.SystemClock; import android.service.wallpaper.WallpaperService; import android.view.MotionEvent; import android.view.SurfaceHolder; /* * This animated wallpaper draws a rotating wireframe cube. */ public class CubeWallpaper1 extends WallpaperService { private final Handler mHandler = new Handler(); Accelero acc; @Override public void onCreate() { super.onCreate(); acc = new Accelero(this); } @Override public void onDestroy() { super.onDestroy(); } ... // skipped to keep post short. }


You have to pass a Activity object to the Accelerometer class and not a WallpaperService object.

Your options to initialize the Accelerometer object:

1) Do it directly from your activity class in the onCreate() method:

Accelerometer accelerometer = new Accelerometer(this);

2) Or you can do it from your WallpaperService class, yet you'd need a reference to your activity class.

Activity foo; Accelerometer accelerometer = new Accelerometer(foo);

You can create a method in your WallpaperService to pass a reference of the activity object to the WallpaperService object.

public void setActivity(Activity foo) { this.foo = foo; }

I hope this helps!


Here's some code to make the second option more understandable:

public class YourWallPaperService extends WallpaperService { Activity foo; // I'm guessing you create a WallpaperService object in your activity code? If so, call this method on that object with a parameter "this" public void setActivity(Activity foo) { this.foo = foo; } }


  • Android GridView item change image on pressed and change back
  • How do I detect the collison of components?
  • Free crop the image android
  • Draw multiple bitmaps on Imageview
  • NullPointerException at Graphics.drawOval [duplicate]
  • How to delay the Drag-Start to combine move and Drag-And-Drop dynamically
  • In Clojure how can I read a public member variables of an instance of a Java class?
  • C# Property Syntax in Java
  • java Affine Transform correct order
  • Restarting the while loop
  • Displaying multiple images without creating new variables [closed]
  • Android Accelerometer setup: can I get it to run only when button it pressed.
  • Can't move Android SeekBar with Appium
  • Android - remove View when its animation finished
  • TranslateTransition does not change (X,Y) co-ordinates
  • Is it possible to hide points on an Androidplot XYplot?
  • How to set google protobuf repeated field in java
  • KeyListener/KeyBinding does not trigger consistently
  • Compiler can't tell which record type with duplicate fields should be the function parameter ty
  • Create JTable from ArrayList
  • Float comparison mismatch
  • How to keep X and Y within layout's view ?
  • android multitouch cant move 2 pointers at same time
  • Find screen angle when orientation is fixed in android
  • Download a file in IE using Selenium
  • Landscape mode UIView.center gives Portrait mode coordinates
  • how to use detect finger swipes on screen on outside of app
  • Android listactivity scrolling with accelerometer issue
  • Sensor Activity in android
  • libgdx distorted texture when using camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
  • Strange jumping values for onTouchListener using a RelativeLayout in Android
  • Move Object along a straight line
  • Showing color selected from an image on touch event in an overlay
  • bquote and evalq unexpected behavior in R-3.2.1
  • Using ifelse with random variate generation in a function applied to a vector
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • Writing to class member through const &
  • Getting coordinates of a component in java
  • How to display a tooltip according to mouse position? - JavaFX
  • Zoom in and out of jPanel