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 <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to