Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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, resetLocationButton;
- //the start time
- private long startTime = 0L;
- //create a handler
- private Handler myHandler = new Handler();
- //timing variables
- long timeInMillies = 0L, timeSwap = 0L, finalTime = 0L;
- LocationManager lm;
- Location loc;
- 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 +
- "\nLast 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);
- resetLocationButton = (Button) findViewById(R.id.resetLocationButton);
- resetLocationButton.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View view)
- {
- //LocationManager
- lm = (LocationManager) getSystemService(LOCATION_SERVICE);
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
- //Location
- loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(loc != null)
- {
- //Set the last latitude and longitude
- currentLon = loc.getLongitude();
- currentLat = loc.getLatitude();
- lastLat = currentLat;
- lastLon = currentLon;
- String message = "Current Location \nLongitude: " + currentLon +
- "\nLatitude: "+ currentLat +
- "\nLast Location \nLongitude: " + lastLon +
- "\nLatitude: "+ lastLat;
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
- }
- }
- });
- 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);
- }
- /**--------------------------------------------NOT USED-----------------------------*/
- @Override
- public void onProviderDisabled(String provider)
- {
- // TODO Auto-generated method stub
- }
- @Override
- public void onProviderEnabled(String provider)
- {
- // TODO Auto-generated method stub
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras)
- {
- // TODO Auto-generated method stub
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment