Guest User

Untitled

a guest
Jun 9th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. public class QuarterMile extends Activity
  2. {
  3.     TextView display;
  4.     TextView display2;
  5.  
  6.     double currentLon = 0;
  7.     double currentLat = 0;
  8.     double lastLon = 0;
  9.     double lastLat = 0;
  10.     double distanceMeters = 0;
  11.    
  12.     //the timer view
  13.     private TextView textTimer;
  14.     //the start finish buttons
  15.     private Button startButton, resetButton, resetLocationButton;
  16.     //the start time
  17.     private long startTime = 0L;
  18.     //create a handler
  19.     private Handler myHandler = new Handler();
  20.     //timing variables
  21.     long timeInMillies = 0L, timeSwap = 0L, finalTime = 0L;
  22.    
  23.     LocationManager lm;
  24.     Location loc;
  25.    
  26.     public void onCreate(Bundle savedInstanceState)
  27.     {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.quarter_mile);
  30.        
  31.         display = (TextView) findViewById(R.id.textView1);
  32.         display2 = (TextView) findViewById(R.id.textView2);
  33.  
  34.         //LocationManager
  35.         lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  36.         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
  37.         //Location
  38.         loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  39.  
  40.         if(loc == null)
  41.         {
  42.             display.setText("No GPS location found");
  43.             Intent i = new Intent(
  44.                      android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
  45.             i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  46.             startActivityForResult(i, 0);
  47.             Toast.makeText(getApplicationContext(),
  48.                     "Please set Mode to 'High accuracy' to enable GPS tracking " +
  49.                     "and then go back to the 1/4-mile timer. Thank you.",
  50.                      Toast.LENGTH_LONG).show();
  51.         }
  52.         else
  53.         {        
  54.             //Set the last latitude and longitude
  55.             currentLon = loc.getLongitude();
  56.             currentLat = loc.getLatitude();
  57.             lastLat = currentLat;
  58.             lastLon = currentLon;
  59.             String message = "Current Location \nLongitude: " + currentLon +
  60.                     "\nLatitude: "+ currentLat +
  61.                              "\nLast Location \nLongitude: " + lastLon +
  62.                     "\nLatitude: "+ lastLat;
  63.             Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  64.         }  
  65.            
  66.         textTimer = (TextView) findViewById(R.id.textTimer);
  67.         startButton = (Button) findViewById(R.id.startButton);
  68.         resetButton = (Button) findViewById(R.id.resetButton);
  69.         resetLocationButton = (Button) findViewById(R.id.resetLocationButton);
  70.        
  71.         resetLocationButton.setOnClickListener(new View.OnClickListener()
  72.         {
  73.             public void onClick(View view)
  74.             {      
  75.                 //LocationManager
  76.                 lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  77.                 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
  78.                 //Location
  79.                 loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  80.                
  81.                 if(loc != null)
  82.                 {
  83.                     //Set the last latitude and longitude
  84.                     currentLon = loc.getLongitude();
  85.                     currentLat = loc.getLatitude();
  86.                     lastLat = currentLat;
  87.                     lastLon = currentLon;
  88.                     String message = "Current Location \nLongitude: " + currentLon +
  89.                         "\nLatitude: "+ currentLat +
  90.                             "\nLast Location \nLongitude: " + lastLon +
  91.                             "\nLatitude: "+ lastLat;
  92.                     Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  93.                 }
  94.             }
  95.         });
  96.  
  97.         startButton.setOnClickListener(new View.OnClickListener()
  98.         {
  99.             public void onClick(View view)
  100.             {
  101.                 startTime = SystemClock.uptimeMillis();
  102.                 myHandler.postDelayed(updateTimerMethod, 0);
  103.             }
  104.         });
  105.            
  106.         resetButton.setOnClickListener(new View.OnClickListener()
  107.         {
  108.             public void onClick(View view)
  109.             {
  110.                 //reset the thread/runnable
  111.                 myHandler.removeCallbacks(updateTimerMethod);
  112.                
  113.                 //reset the timer to 00:00:00
  114.                 int seconds = (int) (0);
  115.                 int minutes = seconds / 60;
  116.                 seconds = seconds % 60;
  117.                 int milliseconds = (int) (0);
  118.                 textTimer.setText("" + minutes + ":"
  119.                         + String.format("%02d", seconds) + ":"
  120.                                 + String.format("%03d", milliseconds));
  121.             }
  122.         });
  123.     }
  124.    
  125.     //start a thread to count the timer
  126.     private Runnable updateTimerMethod = new Runnable()
  127.     {
  128.         public void run()
  129.         {
  130.             timeInMillies = SystemClock.uptimeMillis() - startTime;
  131.             finalTime = timeSwap + timeInMillies;
  132.  
  133.             int seconds = (int) (finalTime / 1000);
  134.             int minutes = seconds / 60;
  135.             seconds = seconds % 60;
  136.             int milliseconds = (int) (finalTime % 1000);
  137.             textTimer.setText("" + minutes + ":"
  138.                     + String.format("%02d", seconds) + ":"
  139.                             + String.format("%03d", milliseconds));
  140.             myHandler.postDelayed(this, 0);
  141.             //if the distance travelled is 400 or more stop the timer
  142.             if(distanceMeters >= 10)
  143.             {
  144.                 timeSwap += timeInMillies;
  145.                 myHandler.removeCallbacks(updateTimerMethod);
  146.                
  147.                 startTime = 0L;
  148.                 timeInMillies = 0L;
  149.                 timeSwap = 0L;
  150.                 finalTime = 0L;
  151.             }
  152.         }
  153.     };
  154.    
  155.     LocationListener Loclist = new LocationListener()
  156.     {
  157.         @Override
  158.         public void onLocationChanged(Location location)
  159.         {  
  160.             //start location manager
  161.             LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  162.             //get last location
  163.             Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  164.             //request new location
  165.             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, Loclist);
  166.  
  167.             //get the current lat and long
  168.             currentLat = loc.getLatitude();
  169.             currentLon = loc.getLongitude();
  170.  
  171.             Location locationA = new Location("point A");
  172.             locationA.setLatitude(lastLat);
  173.             locationA.setLongitude(lastLon);
  174.  
  175.             Location locationB = new Location("point B");
  176.             locationB.setLatitude(currentLat);
  177.             locationB.setLongitude(currentLon);
  178.  
  179.             distanceMeters = locationA.distanceTo(locationB);
  180.             double distanceKm = distanceMeters / 1000f;
  181.  
  182.             display.setText(String.format("%.2f Km", distanceKm));
  183.             display2.setText("Distance is: " + distanceMeters);
  184.         }  
  185.        
  186.        
  187.         /**--------------------------------------------NOT USED-----------------------------*/
  188.  
  189.         @Override
  190.         public void onProviderDisabled(String provider)
  191.         {
  192.             // TODO Auto-generated method stub
  193.         }
  194.  
  195.         @Override
  196.         public void onProviderEnabled(String provider)
  197.         {
  198.             // TODO Auto-generated method stub
  199.         }
  200.  
  201.         @Override
  202.         public void onStatusChanged(String provider, int status, Bundle extras)
  203.         {
  204.             // TODO Auto-generated method stub
  205.         }
  206.     };
  207. }
Advertisement
Add Comment
Please, Sign In to add comment