forked from upstream-mirrors/OpenTracks
Use the handler to update SensorStateActivity
This commit is contained in:
@@ -27,14 +27,12 @@ import com.google.android.maps.mytracks.R;
|
|||||||
import com.google.protobuf.InvalidProtocolBufferException;
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.text.format.DateFormat;
|
import android.text.format.DateFormat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An activity that displays information about sensors.
|
* An activity that displays information about sensors.
|
||||||
*
|
*
|
||||||
@@ -43,12 +41,14 @@ import java.util.TimerTask;
|
|||||||
public class SensorStateActivity extends AbstractMyTracksActivity {
|
public class SensorStateActivity extends AbstractMyTracksActivity {
|
||||||
|
|
||||||
private static final String TAG = SensorStateActivity.class.getName();
|
private static final String TAG = SensorStateActivity.class.getName();
|
||||||
private static final long REFRESH_PERIOD_MS = 250;
|
private static final long ONE_SECOND = 1000;
|
||||||
|
|
||||||
/**
|
private TrackRecordingServiceConnection trackRecordingServiceConnection;
|
||||||
* A Runnable to update the UI.
|
private Handler handler;
|
||||||
*/
|
private SensorManager tempSensorManager;
|
||||||
private final Runnable updateRunnable = new Runnable() {
|
|
||||||
|
private final Runnable updateUiRunnable = new Runnable() {
|
||||||
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ITrackRecordingService trackRecordingService = trackRecordingServiceConnection
|
ITrackRecordingService trackRecordingService = trackRecordingServiceConnection
|
||||||
.getServiceIfBound();
|
.getServiceIfBound();
|
||||||
@@ -69,30 +69,15 @@ public class SensorStateActivity extends AbstractMyTracksActivity {
|
|||||||
stopTempSensorManager();
|
stopTempSensorManager();
|
||||||
updateFromSystemSensorManager();
|
updateFromSystemSensorManager();
|
||||||
}
|
}
|
||||||
|
handler.postDelayed(this, ONE_SECOND);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* A TimeTask to update the UI.
|
|
||||||
*/
|
|
||||||
private class UpdateTimerTask extends TimerTask {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (isVisible) {
|
|
||||||
runOnUiThread(updateRunnable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private TrackRecordingServiceConnection trackRecordingServiceConnection;
|
|
||||||
private boolean isVisible = false;
|
|
||||||
private Timer timer = null;
|
|
||||||
private SensorManager tempSensorManager = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, null);
|
trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, null);
|
||||||
|
handler = new Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -104,18 +89,13 @@ public class SensorStateActivity extends AbstractMyTracksActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
isVisible = true;
|
handler.post(updateUiRunnable);
|
||||||
timer = new Timer();
|
|
||||||
timer.schedule(new UpdateTimerTask(), 0, REFRESH_PERIOD_MS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
isVisible = false;
|
handler.removeCallbacks(updateUiRunnable);
|
||||||
timer.cancel();
|
|
||||||
timer.purge();
|
|
||||||
timer = null;
|
|
||||||
stopTempSensorManager();
|
stopTempSensorManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user