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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:49:14+00:00 2026-06-09T14:49:14+00:00

I got this Accelerometer class that holds values of accelerometer and I can acces

  • 0

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.
}
  • 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-09T14:49:15+00:00Added an answer on June 9, 2026 at 2:49 pm

    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!

    Update:

    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;
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
Got this regex string from JavaScript: the good parts (pp. 66). Can't get it
got this code from a website that helped me in creating buttons and stuff.
I got this example from one StackOverflow question that was asked but I couldn't
Got this from the encoding example in ffmpeg. I can somewhat follow the authors
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this: SELECT deals.* FROM deals JOIN deals_bookings ON (deals_bookings.deal_id = deals.ID) WHERE
i got this mssql query... but then i want to use it in mysql..
I got this piece of code from Mysql 4th edition to check table existence
Got this: http://jsfiddle.net/NdsF8/ Want it to be dynamic so when you click a new

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.