package com.example.pace; import android.app.Activity; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.math.BigDecimal; public class MainActivity extends Activity implements GPSCallback { private GPSManager gpsManager = null; private double speed = 0.0; Boolean isGPSEnabled=false; LocationManager locationManager; double currentSpeed,kmphSpeed, mileSpeed; TextView txtview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtview=(TextView)findViewById(R.id.info); try { if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 101); } } catch (Exception e){ e.printStackTrace(); } getCurrentSpeed(txtview); } public void getCurrentSpeed(View view){ txtview.setText(getString(R.string.info)); locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); gpsManager = new GPSManager(MainActivity.this); isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if(isGPSEnabled) { gpsManager.startListening(getApplicationContext()); gpsManager.setGPSCallback(this); } else { gpsManager.showSettingsAlert(); } } @Override public void onGPSUpdate(Location location) { speed = location.getSpeed(); currentSpeed = round(speed,3,BigDecimal.ROUND_HALF_UP); kmphSpeed = round((currentSpeed*3.6),3, BigDecimal.ROUND_HALF_UP); mileSpeed = kmphSpeed/1.609344; String s = Double.toString(mileSpeed); double d = Double.parseDouble(s.charAt(0) + "." + s.charAt(s.indexOf('.') + 1)); txtview.setText(d+ ""); } @Override protected void onDestroy() { gpsManager.stopListening(); gpsManager.setGPSCallback(null); gpsManager = null; super.onDestroy(); } public static double round(double unrounded, int precision, int roundingMode) { BigDecimal bd = new BigDecimal(unrounded); BigDecimal rounded = bd.setScale(precision, roundingMode); return rounded.doubleValue(); } }