阅读传感器,而屏幕是黑 [英] Read sensors while screen is black

查看:217
本文介绍了阅读传感器,而屏幕是黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨这里还有一个有趣的问题!

Hi here there is a interesting problem!

我有这样的服务:

public class SensorService extends Service implements SensorEventListener {
private static final String TAG = "MyService";
private MyServiceBinder myServiceBinder = new MyServiceBinder();
private SensorManager mSensorManager;
          ...........
private PowerManager.WakeLock wl;
private Handler mPeriodicEventHandler;  //Per interrompere la trasformata a tempo
private int PERIODIC_EVENT_TIMEOUT = 5000;
private int x=0,mediacount=0;

@Override
public IBinder onBind(Intent intent) {
    return myServiceBinder; // object of the class that implements Service interface.
}
public class MyServiceBinder extends Binder implements IMyService {
    public void set(String tr, int cp, int sd) {
        ......
    }
}
@Override
public void onCreate() {
    Log.d(TAG, "onCreate");

    // Get the Wake Lock
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK,
            "wlTag");
    wl.acquire();

    // Get the SensorManager 
    mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensorManager.registerListener(this,
            mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),
            sDelay);

        recc =  new recClass(this);
    recc.open();

    // To execute every PERIODIC_EVENT_TIMEOUT seconds
    mPeriodicEventHandler = new Handler();
    mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
}

@Override
public void onDestroy() {
    Log.d(TAG, "onDestroy");
    mIsStarted = false;
    mPeriodicEventHandler.removeCallbacks(doPeriodicTask);
    recc.close();
    wl.release();
}

@Override
public void onSensorChanged(SensorEvent event) {    // SensorEventListener
    Sensor sens = event.sensor;
    if ((sens.getType() == Sensor.TYPE_ACCELEROMETER) && mIsStarted){
            fft.add((float)Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2])));   // Add value to the fft
            dataBuffer = fft.calculate();
            if (dataBuffer != null){
                for (int i=0; i<(fft.camp/2);i++)
                        recc.addValue(toRec, dataBuffer[i]);
                x++;
                Toast.makeText(this, "FFT: "+x, Toast.LENGTH_SHORT).show();
                mIsStarted = false;
                // mIsStarted will be true again after PERIODIC_EVENT_TIMEOUT milliseconds
                mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
            }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private Runnable doPeriodicTask = new Runnable()
{
    public void run() 
    { 
        mIsStarted = true;
    }
};

}

怎么可以看到,我收到了PARTIAL_WAKE_LOCK,我采取的是由x变量多少FFT改造中做计数。

How you can see i get a PARTIAL_WAKE_LOCK and i take count of how many FFT trasformation it do by the x variable.

所以启动服务后,我变成黑色的屏幕pressing红色电话按钮,过了一会儿,我试试,看看有多少FFT已经完成它,但是x的最后一个值是一样的,当我变成了黑色画面。

So after starting the service, i turn black the screen pressing the red phone button, and after a while i try to see how many FFT have it done, but the last value of x is the same of when i turned black the screen.

一些实验我发现后,如果我尝试PARTIAL_WAKE_LOCK,而不运行的好方法onSensorChanged传递,但似乎当屏幕变黑,everithing继续除外传感器监听器运行。我怎样才能修复这个?

After some experiment i discovered that if i try the PARTIAL_WAKE_LOCK, without passing from the onSensorChanged method it runs good, but it seems that when the screen go black, everithing continue running except for the sensor listener. How can i repair at this?

Tnks

瓦莱里奥

推荐答案

这似乎是一个错误的Andr​​oid

It seems to be a Android Bug

HTTP://$c$c.google。 COM / p /安卓/问题/细节?ID = 3708

看来只有部分上解析的Andr​​oid 2.2

It seems only partially resolved on android 2.2

这篇关于阅读传感器,而屏幕是黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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