[android-developers] Re: SensorEvent timestamp doesn't always update
Hi again, sorry to spam. I just found the following note in http://developer.android.com/reference/android/hardware/SensorEventListener.html#onSensorChanged(android.hardware.SensorEvent) NOTE: The application doesn't own the event object passed as a parameter and therefore cannot hold on o it. The object may be part of an internal pool and may be reused by the framework. I don't know how I missed that, which explains the problem. Thanks again for your help. Regards, Markus On Oct 28, 10:38 pm, josean1968 wrote: > Hello, > > You have this: > > ... > private SensorEvent lastEvent; > ... > > @Override > public void onSensorChanged(SensorEvent event) { > if (lastEvent == null) { > lastEvent = event; > return; > } > > Log.v(TAG, "dT [" + Thread.currentThread().getName() + "]: " + > (event.timestamp - lastEvent.timestamp)); > } > > > > The problem is that the "onSensorChanged()" method can be invoked > concurrently because it is an event listener. > Declare a local variable and copy the value (not just a pointer to the > object): > > @Override > public void onSensorChanged(SensorEvent event) { > ... > int lastTimestamp = event.timestamp; > ... > } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: SensorEvent timestamp doesn't always update
Hi, I only saw your answer today, thanks for it. It appears do make a difference, now it works. I still don't get why, though. The method always gets invoked by the same thread (which I've checked by outputting Thread.currentThread().getName() to the log), so it doesn't get invoked concurrently (because that's impossible if only one thread is involved). Even if I synchronize the method invocation on the listener object, it doesn't make a difference when storing only the object pointer. Where's the flaw in my reasoning? Thanks again! Regards, Markus On Oct 28, 10:38 pm, josean1968 wrote: > Hello, > > You have this: > > ... > private SensorEvent lastEvent; > ... > > @Override > public void onSensorChanged(SensorEvent event) { > if (lastEvent == null) { > lastEvent = event; > return; > } > > Log.v(TAG, "dT [" + Thread.currentThread().getName() + "]: " + > (event.timestamp - lastEvent.timestamp)); > } > > > > The problem is that the "onSensorChanged()" method can be invoked > concurrently because it is an event listener. > Declare a local variable and copy the value (not just a pointer to the > object): > > @Override > public void onSensorChanged(SensorEvent event) { > ... > int lastTimestamp = event.timestamp; > ... > } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: SensorEvent timestamp doesn't always update
Hello, You have this: ... private SensorEvent lastEvent; ... @Override public void onSensorChanged(SensorEvent event) { if (lastEvent == null) { lastEvent = event; return; } Log.v(TAG, "dT [" + Thread.currentThread().getName() + "]: " + (event.timestamp - lastEvent.timestamp)); } The problem is that the "onSensorChanged()" method can be invoked concurrently because it is an event listener. Declare a local variable and copy the value (not just a pointer to the object): @Override public void onSensorChanged(SensorEvent event) { ... int lastTimestamp = event.timestamp; ... } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en