Reputation: 11
I have created a speedometer application, I want it to display the max speed the user has achieved. How do I find the highest speed the user has achieved?
I have already tried to contain the speeds in an array so that I can find the highest speed using the math.max function but the speeds in a variable keep changing how do I store the past speeds to compare and find the highest speed.
my main3activity:
public class Main3Activity extends AppCompatActivity implements android.location.LocationListener {
int arr[];
public int borat;
public float boo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
LocationManager lm =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
this.onLocationChanged(null);
final TextView mTextField=(TextView)this.findViewById(R.id.textView);
new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
Intent myIntent = new Intent(Main3Activity.this,
aftergame.class);
startActivity(myIntent);
}
}.start();
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
@Override
public void onLocationChanged(Location location) {
int cpeeda;
int cpeedb;
final int speed;
TextView txt=(TextView)this.findViewById(R.id.st);
if(location==null){
txt.setText("0 m/s");
}else{
float cpeed=location.getSpeed();
float cpeed1=location.getSpeed();
cpeeda=(int)cpeed;
cpeeda=arr[0];
borat= Math.max(0,arr[0]);
txt.setText(cpeeda + " m/s");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
my activity which displays the high speed:
public class aftergame extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aftergame);
button = (Button) findViewById(R.id.button2) ;
Main3Activity m=new Main3Activity();
TextView tm=(TextView)this.findViewById(R.id.textView3);
tm.setText(""+m.borat);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(aftergame.this,
MainActivity.class);
startActivity(myIntent);
}
});
}
}
Upvotes: 1
Views: 178
Reputation: 140553
Here:
cpeeda=arr[0];
borat= Math.max(0,arr[0]);
That is non-sensical. I guess you meant to assign
arr[0] = cpeeda;
But even that doesn't make much sense. The idea of arrays is that they provide multiple data points. When you only assign a value into the first slot, all the other array slots stay at their initial value (which would be 0 if done right). Btw: your code does not create an array, so arr
is null first of all.
In the end, the real answer is two-fold:
Simply do something like:
double maxSpeed = 0; // some field of your class, similar to your array
... wherever you determine the current speed:
if (maxSpeed > currentSpeed) {
maxSpeed = currentSpeed;
Yet, when you want to store multiple datapoints, you either should create an array of a fixed size (where you start overwriting older values once you got to that fixed size), or you could use an ArrayList. That one grows dynamically, thus you could just keep adding values to it. (of course, at some point you better stop doing so, otherwise you will run out of memory sooner or later)
Upvotes: 1