Cześć. Mam pewien problem z aplikacją, która mierzy pokonany dystans. W głównym menu aplikacja ma 4 przyciski - jednym z nich jest przycisk "BIEGNIJ", który przechodzi do nowej aktywności Drogomierz.W tej aktywności mam 3 przyciski - start, stop i pauza. Po wciśnięciu START aplikacja prawidłowo mierzy dystans, problem jest z jej wstrzymanie.Chciałbym aby po wciśnięciu przycisku "pauza" aplikacja przestała mierzyć dystans. Drugim problemem jest zakończenie mierzenia, a później jego ponowne włączenie - gdy zakończe mierzenie dystansu, aplikacja wyświetla okno z informacjami na temat pokonanego dystansu, czasu, prędkości oraz przycisk powrotu do głównego menu. Gdy wróce do głównego menu, po czym znów wejde w drogomierz i kliknę start to aplikacja zaczyna mierzyć dystans od tegodystansuprzy którym wcześniej zakończyłem pomiar. Bardzo prosiłbym o pomoc.
Tutaj kod:
public class runActivity extends AppCompatActivity {
public String speedString;
public String distanceStr;
private OdometerService odometer;
private boolean bound = false;
private double distance = 0.0;
private double speed = 0.0;
private int seconds = 0;
private boolean running;
public String time;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
OdometerService.OdometerBinder odometerBinder = (OdometerService.OdometerBinder) binder;
odometer = odometerBinder.getOdometer();
bound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
bound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_run);
watchMileage();
runTimer();
}
private void watchMileage(){
final TextView speedView = findViewById(R.id.speed_view);
final TextView distanceView = findViewById(R.id.distance);
final Handler handler = new Handler();
handler.post(new Runnable() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
if(odometer != null){
distance = odometer.getDistance();
speed = odometer.getSpeed();
}
distanceStr = String.format("%1$,.3f km", distance);
distanceView.setText(distanceStr);
speedString = String.format("%1$,.3f km/h", speed);
speedView.setText(speedString);
handler.postDelayed(this, 1000);
}
});
}
void bindService(){
if(bound)
return;
Intent intent = new Intent(this, OdometerService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
bound = true;
distance = 0;
}
void unbindService(){
if(!bound)
return;
Intent intent = new Intent(this, OdometerService.class);
unbindService(connection);
bound = false;
}
public void onClickStart(View view){
bindService();
running = true;
}
public void onClickStop(View view){
if(bound) unbindService();
running = false;
Intent runResultIntent = new Intent(this, runResultActivity.class);
runResultIntent.putExtra("time", time);
runResultIntent.putExtra("distanceStr", distanceStr);
runResultIntent.putExtra("seconds", seconds);
runResultIntent.putExtra("distance", distance);
startActivity(runResultIntent);
}
public void onClickPause(View view){
unbindService(connection);
bound = false;
running = false;
}
private void runTimer(){
final TextView timeView = findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds%3600)/60;
int secs = seconds%60;
time = String.format("%d:%02d:%02d", hours, minutes, secs);
timeView.setText(time);
if(running){
seconds++;
}
handler.postDelayed(this, 1000);
}
});
}
}