从WallpaperService调用加速度计 [英] Calling accelerometer from WallpaperService

查看:259
本文介绍了从WallpaperService调用加速度计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我持有加速度值这个Accelerometer类,我可以从任何其他类存取权限他们每当我想。通常我会创建新的对象加速度加速度=新的加速度计(本); 但是当我在 WallpaperService 它doesn'别让我用这个作为参数。<​​/ p>

下面是Acclerometer类:

 进口android.app.Activity;
进口android.content.Context;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;公共类的Accelero实现SensorEventListener {    私人浮动XAXIS;
    私人浮动Y轴;
    私人浮动Z轴;    的SensorManager经理;
    sensor重力感应;
    活动活动;    公众的Accelero(活动活动){
        this.activity =活动;
        经理=(的SensorManager)this.activity.getSystemService(Context.SENSOR_SERVICE);
        加速度= manager.getSensorList(Sensor.TYPE_ACCELEROMETER)获得(0);
        manager.registerListener(这一点,加速度计,SensorManager.SENSOR_DELAY_GAME);
     }
    公众持股量的getX(){
        返回this.xAxis;
    }    公众持股量的getY(){
        返回this.yAxis;
    }    公众持股量盖茨(){
        返回this.zAxis;
    }    公共无效onAccuracyChanged(传感器传感器,精度INT){
        // TODO自动生成方法存根    }    公共无效onSensorChanged(SensorEvent事件){
            XAXIS = event.values​​ [0];
            Y轴= event.values​​ [1];
            Z轴= event.values​​ [2];
    }}

例如我试图从与SDK附带的样本code访问它时,CubeWallpaper

 进口com.example.android.livecubes.R;进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.os.Handler;
进口android.os.SystemClock;
进口android.service.wallpaper.WallpaperService;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;/ *
 *本动画壁纸绘制一个旋转的立方体线框。
 * /
公共类CubeWallpaper1扩展WallpaperService {    私人最终处理程序mHandler =新的处理程序();
    ACC的Accelero;    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        ACC =新的Accelero(本);
    }    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
    }    ... //跳过,以保持后做空。
}


解决方案

您有一个活动对象传递来Accelerometer类,而不是一个 WallpaperService对象

您选择初始化Accelerometer对象:

1)直接它从你的活动类的onCreate()方法:

 加速度加速度=新加速计(本);

2),或者你可以从你的WallpaperService类做到这一点,但你需要你的活动类的引用。

 活动foo的;加速度计加速度=新加速计(富);

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

 公共无效setActivity(活动富){
this.foo = foo的;
}

我希望这有助于!

更新:

下面是一些code键使第二个选项更容易理解:

 公共类YourWallPaperService扩展WallpaperService {
活动foo的;//我猜你创建你的活动code一WallpaperService对象吗?如果是这样,调用该对象的此方法与参数这
公共无效setActivity(活动富){
this.foo = foo的;
}}

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!

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

}

这篇关于从WallpaperService调用加速度计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆