问题
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:
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;
}
}
来源:https://stackoverflow.com/questions/11922453/calling-accelerometer-from-wallpaperservice