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.
}
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67
  • When you write 'this' you mean the current class. your Accelero's constructor expects an Activity. but you pass it a Service. I don't really know services, but this might help you http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service – La bla bla Aug 12 '12 at 13:31

1 Answers1

1

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;
}

}
Luke Taylor
  • 9,481
  • 13
  • 41
  • 73
  • but there is no activity in this project. Its only public class CubeWallpaper1 that extends WallpaperService. I thought wallpapers didnt include any activity classes, am I wrong? – Rohit Malish Aug 12 '12 at 14:07
  • i have nothing that extends activity. its just one class that extends WallPaperService – Rohit Malish Aug 12 '12 at 14:21
  • I haven't done anything with the WallpaperService, so I'm not quiet sure if you can register a sensor (Sorry, after some research I found out what it is). (There should be some way though. I'll try and find out). – Luke Taylor Aug 12 '12 at 14:30
  • but thanks for help :) im searching for a way to do it too. ill answer here if I find one. – Rohit Malish Aug 12 '12 at 14:39