为什么加速度计没有按时启动或正常检索数据?

发布于 2024-12-14 21:56:46 字数 1749 浏览 3 评论 0原文

我编写了一个应用程序,计划每 15 秒启动一次加速度计并运行 10 秒以检索传感器数据。我使用 AlarmManager 启动加速度计,并在屏幕关闭时通过唤醒锁保持其运行。但我发现有时

  1. 加速度计在一段时间后无法检索数据。我的意思是它已经根据logcat启动了,但是没有任何传感器数据,然后它被关闭并在没有数据的情况下再次启动。
  2. 我不知道为什么加速度计在一段时间后不再启动,而之前一切都很好。我发现 logcat 说“带有绑定器 android.os.BinderProxy 的监听器不存在加速计”。我不知道为什么。

以下是内核代码:

Activity.java

Intent intent = new Intent(getApplicationContext(),SensorService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long curTime = Calendar.getInstance().getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, curTime, _intervalTime, pendingIntent);  

SensorService.java

public void onStart(Intent intent, int startId) {

        new Thread(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
                wl.acquire();
                Log.d(TAG,"I'm bright!");
                try {
                    Thread.sleep(_delayClose);// kill self after _delayClose ms
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stopSelf();// SensorService.this.stopSelf()
            }

        }).start();

        this.mSensorManager.registerListener( _sensorHandle, 
                accSensor, 
                sensorDelay)
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    this.mSensorManager.unregisterListener(_sensorHandle);
    Log.d(TAG,"TIEM UP");
    if(wl.isHeld())
    {
        wl.release();   
    }
}

I write an app that schedule to start accelerometer every 15 seconds and run it 10 seconds to retrieve sensor data. I start accelerometer using alarmManager and keep it running with wakelock when screen go off. But I found sometimes:

  1. The accelerometer can't retrieve data after some periods. I mean it have been started according to logcat,but there is not any sensor data,then it is closed and it starts again without data.
  2. I don't know why the accelerometer didn't start any more after some periods and it is fine before. I found logcat says "listener with binder android.os.BinderProxy doesn't exist accelerometer". I don't know why.

following is kernel code:

Activity.java

Intent intent = new Intent(getApplicationContext(),SensorService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long curTime = Calendar.getInstance().getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, curTime, _intervalTime, pendingIntent);  

SensorService.java

public void onStart(Intent intent, int startId) {

        new Thread(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
                wl.acquire();
                Log.d(TAG,"I'm bright!");
                try {
                    Thread.sleep(_delayClose);// kill self after _delayClose ms
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stopSelf();// SensorService.this.stopSelf()
            }

        }).start();

        this.mSensorManager.registerListener( _sensorHandle, 
                accSensor, 
                sensorDelay)
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    this.mSensorManager.unregisterListener(_sensorHandle);
    Log.d(TAG,"TIEM UP");
    if(wl.isHeld())
    {
        wl.release();   
    }
}

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

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

发布评论

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

评论(1

蓝天 2024-12-21 21:56:46

我猜测经常注册和取消注册传感器侦听器一定会产生相当大的开销,再加上alarmManager的开销。

考虑到您经常需要传感器数据,那么如何让您的侦听器注册并让它在内部管理它何时应该和不应该对它接收的样本执行一些有用的操作?

public class SensorSamplrActivity extends Activity {
    private final static String TAG = "samplr";
    private final static int SAMPLE_INTERVAL_SECS = 15;
    private final static int SAMPLE_DURATION_SECS = 10;

    private SensorManager mSensorManager;
    private long whenToStartSample = System.currentTimeMillis();
    private long whenToEndSample = System.currentTimeMillis() + SAMPLE_DURATION_SECS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
    }

    private SensorEventListener mSensorListener = new SensorEventListener() {

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            long now = System.currentTimeMillis();
            if (now < whenToStartSample){
                // ignore the event & wait for next time to sample
                Log.d(TAG,"ignoring events for " + (whenToStartSample - now) + "ms");
                return;
            }

            if (whenToStartSample <= now && whenToEndSample > now){
                Log.d(TAG,"Do something with this event @ " + now);
            }
            else {
                // we've gone past whenToEndSample so reset timers
                whenToStartSample = now + (SAMPLE_INTERVAL_SECS * 1000);
                whenToEndSample = whenToStartSample + (SAMPLE_DURATION_SECS * 1000) ;
            }
        }
    };

    @Override
    public void onDestroy(){
        super.onDestroy();
        mSensorManager.unregisterListener(mSensorListener);
    }
}

I'm guessing that there must be some considerable overhead in registering and unregistering sensor listener that often, plus the alarmManager overhead.

Given that you want sensor data that often, how about just keep your listener registered and have it internally manage when it should and shouldnt do something useful with the samples it's receiving?

public class SensorSamplrActivity extends Activity {
    private final static String TAG = "samplr";
    private final static int SAMPLE_INTERVAL_SECS = 15;
    private final static int SAMPLE_DURATION_SECS = 10;

    private SensorManager mSensorManager;
    private long whenToStartSample = System.currentTimeMillis();
    private long whenToEndSample = System.currentTimeMillis() + SAMPLE_DURATION_SECS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
    }

    private SensorEventListener mSensorListener = new SensorEventListener() {

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            long now = System.currentTimeMillis();
            if (now < whenToStartSample){
                // ignore the event & wait for next time to sample
                Log.d(TAG,"ignoring events for " + (whenToStartSample - now) + "ms");
                return;
            }

            if (whenToStartSample <= now && whenToEndSample > now){
                Log.d(TAG,"Do something with this event @ " + now);
            }
            else {
                // we've gone past whenToEndSample so reset timers
                whenToStartSample = now + (SAMPLE_INTERVAL_SECS * 1000);
                whenToEndSample = whenToStartSample + (SAMPLE_DURATION_SECS * 1000) ;
            }
        }
    };

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