0

我得到了这个加速度计类,它保存加速度计的值,我可以随时从任何其他类访问它们。通常我会创建新对象Accelerometer accelerometer = new Accelerometer(this); ,但是当我在里面时WallpaperService,它不允许我this用作参数。

这是加速度计类:

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

}

例如,我尝试从 SDK 附带的示例代码 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.
}
4

1 回答 1

1

您必须将 a 传递Activity object给 Accelerometer 类,而不是 a WallpaperService object

初始化 Accelerometer 对象的选项:

1) 直接从您的活动类的 onCreate() 方法中执行此操作:

Accelerometer accelerometer = new Accelerometer(this);

2)或者您可以从您的 WallpaperService 类中执行此操作,但您需要引用您的活动类。

Activity foo;

Accelerometer accelerometer = new Accelerometer(foo);

您可以在 WallpaperService 中创建一个方法,以将活动对象的引用传递给 WallpaperService 对象。

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

我希望这有帮助!

更新:

这里有一些代码可以让第二个选项更容易理解:

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

}
于 2012-08-12T13:49:14.060 回答