I have a class that is to be a 1/4 mile timer.
The code is below:
My question is:
After the user has enabled GPS (if it's disabled) and goes back to the application the distance is 5800+km instead of being 0.00 like it is when it is opened up when GPS is already enabled.
Is there a way of reloading the onCreate() when the user presses back?
Or is there a way of updating the variables?
The code is below:
public class QuarterMile extends Activity
{
TextView display;
TextView display2;
double currentLon = 0;
double currentLat = 0;
double lastLon = 0;
double lastLat = 0;
double distanceMeters = 0;
//the timer view
private TextView textTimer;
//the start finish buttons
private Button startButton, resetButton;
//the start time
private long startTime = 0L;
//create a handler
private Handler myHandler = new Handler();
//timing variables
long timeInMillies = 0L, timeSwap = 0L, finalTime = 0L;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.quarter_mile);
display = (TextView) findViewById(R.id.textView1);
display2 = (TextView) findViewById(R.id.textView2);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc == null)
{
display.setText("No GPS location found");
Intent i = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(i, 0);
Toast.makeText(getApplicationContext(),
"Please set Mode to 'High accuracy' to enable GPS tracking " +
"and then go back to the 1/4-mile timer. Thank you.",
Toast.LENGTH_LONG).show();
}
else
{
//Set the last latitude and longitude
currentLon = loc.getLongitude();
currentLat = loc.getLatitude();
lastLat = currentLat;
lastLon = currentLon;
String message = "Current Location \nLongitude: " + currentLon +
"\nLatitude: "+ currentLat +
"\nCurrent Location \nLongitude: " + lastLon +
"\nLatitude: "+ lastLat;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
textTimer = (TextView) findViewById(R.id.textTimer);
startButton = (Button) findViewById(R.id.startButton);
resetButton = (Button) findViewById(R.id.resetButton);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
});
resetButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
//reset the thread/runnable
myHandler.removeCallbacks(updateTimerMethod);
//reset the timer to 00:00:00
int seconds = (int) (0);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (0);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
}
});
}
//start a thread to count the timer
private Runnable updateTimerMethod = new Runnable()
{
public void run()
{
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
//if the distance travelled is 400 or more stop the timer
if(distanceMeters >= 10)
{
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
startTime = 0L;
timeInMillies = 0L;
timeSwap = 0L;
finalTime = 0L;
}
}
};
LocationListener Loclist = new LocationListener()
{
@override
public void onLocationChanged(Location location)
{
//start location manager
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
//get last location
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//request new location
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
//get the current lat and long
currentLat = loc.getLatitude();
currentLon = loc.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(lastLat);
locationA.setLongitude(lastLon);
Location locationB = new Location("point B");
locationB.setLatitude(currentLat);
locationB.setLongitude(currentLon);
distanceMeters = locationA.distanceTo(locationB);
double distanceKm = distanceMeters / 1000f;
display.setText(String.format("%.2f Km", distanceKm));
display2.setText("Distance is: " + distanceMeters);
}
{
TextView display;
TextView display2;
double currentLon = 0;
double currentLat = 0;
double lastLon = 0;
double lastLat = 0;
double distanceMeters = 0;
//the timer view
private TextView textTimer;
//the start finish buttons
private Button startButton, resetButton;
//the start time
private long startTime = 0L;
//create a handler
private Handler myHandler = new Handler();
//timing variables
long timeInMillies = 0L, timeSwap = 0L, finalTime = 0L;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.quarter_mile);
display = (TextView) findViewById(R.id.textView1);
display2 = (TextView) findViewById(R.id.textView2);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc == null)
{
display.setText("No GPS location found");
Intent i = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(i, 0);
Toast.makeText(getApplicationContext(),
"Please set Mode to 'High accuracy' to enable GPS tracking " +
"and then go back to the 1/4-mile timer. Thank you.",
Toast.LENGTH_LONG).show();
}
else
{
//Set the last latitude and longitude
currentLon = loc.getLongitude();
currentLat = loc.getLatitude();
lastLat = currentLat;
lastLon = currentLon;
String message = "Current Location \nLongitude: " + currentLon +
"\nLatitude: "+ currentLat +
"\nCurrent Location \nLongitude: " + lastLon +
"\nLatitude: "+ lastLat;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
textTimer = (TextView) findViewById(R.id.textTimer);
startButton = (Button) findViewById(R.id.startButton);
resetButton = (Button) findViewById(R.id.resetButton);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
});
resetButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
//reset the thread/runnable
myHandler.removeCallbacks(updateTimerMethod);
//reset the timer to 00:00:00
int seconds = (int) (0);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (0);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
}
});
}
//start a thread to count the timer
private Runnable updateTimerMethod = new Runnable()
{
public void run()
{
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
//if the distance travelled is 400 or more stop the timer
if(distanceMeters >= 10)
{
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
startTime = 0L;
timeInMillies = 0L;
timeSwap = 0L;
finalTime = 0L;
}
}
};
LocationListener Loclist = new LocationListener()
{
@override
public void onLocationChanged(Location location)
{
//start location manager
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
//get last location
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//request new location
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
//get the current lat and long
currentLat = loc.getLatitude();
currentLon = loc.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(lastLat);
locationA.setLongitude(lastLon);
Location locationB = new Location("point B");
locationB.setLatitude(currentLat);
locationB.setLongitude(currentLon);
distanceMeters = locationA.distanceTo(locationB);
double distanceKm = distanceMeters / 1000f;
display.setText(String.format("%.2f Km", distanceKm));
display2.setText("Distance is: " + distanceMeters);
}
My question is:
After the user has enabled GPS (if it's disabled) and goes back to the application the distance is 5800+km instead of being 0.00 like it is when it is opened up when GPS is already enabled.
Is there a way of reloading the onCreate() when the user presses back?
Or is there a way of updating the variables?