屏幕黑时读取传感器

发布于 2024-09-27 11:21:44 字数 3089 浏览 5 评论 0原文

你好,这里有一个有趣的问题!

我有这个服务:

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 转换。

因此,启动服务后,我按下红色电话按钮将屏幕变黑,过了一会儿我尝试查看它完成了多少次 FFT,但 x 的最后一个值与我变黑屏幕时的值相同。

经过一些实验,我发现如果我尝试 PARTIAL_WAKE_LOCK,而不通过 onSensorChanged 方法,它运行良好,但似乎当屏幕变黑时,除了传感器侦听器之外,一切都会继续运行。我该如何修复这个?

瓦莱里奥

Hi here there is a interesting problem!

I have this service:

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

}

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.

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.

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

Valerio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄暮涼年 2024-10-04 11:21:45

这似乎是一个 Android 错误

http://code.google.com/ p/android/issues/detail?id=3708

在 android 2.2 上似乎只部分解决

It seems to be a Android Bug

http://code.google.com/p/android/issues/detail?id=3708

It seems only partially resolved on android 2.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文