我已经创建了一个速度表应用程序,我希望它显示用户已达到的最大速度。如何找到用户达到的最高速度?
我已经尝试将速度包含在数组中,以便可以使用math.max函数找到最高速度,但是变量中的速度不断变化,如何存储过去的速度以进行比较并找到最高速度
我的main3活动:
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) {
}
}
显示高速度的活动:
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);
}
});
}
}
答案 0 :(得分:1)
这里:
cpeeda=arr[0];
borat= Math.max(0,arr[0]);
那是荒谬的。我猜你打算分配
arr[0] = cpeeda;
但是即使那样也没有多大意义。数组的想法是它们提供多个数据点。当您仅将一个值分配给第一个插槽时,所有其他阵列插槽均保持其初始值(如果正确执行,则为0)。顺便说一句:您的代码不会不创建数组,因此arr
首先是 null 。
最后,真正的答案有两个:
只需执行以下操作:
double maxSpeed = 0; // some field of your class, similar to your array
...确定当前速度的任何地方:
if (maxSpeed > currentSpeed) {
maxSpeed = currentSpeed;
但是,当您要存储多个数据点时,您应该创建一个固定大小的数组(一旦达到该固定大小,便开始覆盖较早的值),或者可以使用ArrayList。那是动态增长的,因此您可以继续为其添加价值。 (当然,最好在某些时候停止这样做,否则迟早会耗尽内存)