My android app Main Activity
is using a Thread
because a very "heavy" algorithm is being used and without threading it just stacks my UI and app.
That's my Thread
:
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimuth_angle = (float) (orientation[0]*180/Math.PI);
pitch_angle = (float) (orientation[1]*180/Math.PI);
roll_angle = (float) (orientation[2]*180/Math.PI);
p.setText(String.valueOf(pitch_angle));
r.setText(String.valueOf(roll_angle));
y.setText(String.valueOf(azimuth_angle));
new Thread(new Runnable() {
public void run()
{
try
{
Locations = Algo( pitch_angle, roll_angle,
azimuth_angle);
Thread.sleep(500);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
Problem:
Algo time is about 150-300 milliSeconds. I think this thread is activated again before Algo is finished. because the Thread
is executed each time onSensorChanged
running.
What can I do to make Algo return it's values to "Locations" as expected?
P.S Algo is tested on a Service
and works properly.