我想用开始停止按钮制作实时速度计

时间:2019-05-28 07:53:06

标签: android

我正在制作一个带有位置侦听器的速度计应用程序,我添加了一个按钮来停止速度,并且在活动打开时它将自动获得速度。现在的问题是,当我坐在一个地方时,即使按下停止按钮,它也可以计算速度

public class SpeedoMeter Activity extends AppCompatActivity implements LocationListener {

    SwitchCompat switchCompat;
    TextView textView;
    ImageSpeedometer speedometer;
    FloatingActionButton button;
    CLocation cLocation;
    boolean stopCheck = false;
    Location mLocation;
    double speed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speed_meter);

        this.updateSpeed(null);
        Toast.makeText(this, "Start moving", Toast.LENGTH_SHORT).show();

        button = findViewById(R.id.startStop);
        switchCompat = findViewById(R.id.switcher);
        textView = findViewById(R.id.speed);
        speedometer = findViewById(R.id.imageSpeedometer);
        speedometer.setIndicatorLightColor(Color.WHITE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                != (PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
        } else {
            goNext();
        }
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateSpeed(null);
            }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!stopCheck) {
                    stop();
                }else{
                    goNext();
                }

            }
        });
    }

    @Override
    public void onLocationChanged(final Location location) {
        mLocation = location;
        if (mLocation != null && location.hasSpeed()) {
            updateSpeed(null);
            cLocation = new CLocation(mLocation, useMetricUnits());
            updateSpeed(cLocation);
        }

        if (!stopCheck) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stop();
                }
            });
        }
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        updateSpeed(null);
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    public void goNext() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
    }

    public void stopSpeed(CLocation location) {
        if (location != null) {
            speedometer.speedTo(location.getSpeed() * 0);
            textView.setText(new DecimalFormat("###.#").format(location.getSpeed() * 0) + " Kms/h ");
        }
    }

    public void stop(){
        stopSpeed(cLocation);
        stopCheck = true;
    }

    public void updateSpeed(CLocation location){
        if(location != null){
            location.setUseMatricUnits(this.useMetricUnits());

            if(this.useMetricUnits()){
                speedometer.speedTo(location.getSpeed() * 2.23694f);
                textView.setText(new DecimalFormat("###.#").format(location.getSpeed()*3.6f) + " Kms/h ");
            }else {
                speedometer.speedTo(location.getSpeed());
                textView.setText(new DecimalFormat("###.#").format(location.getSpeed()*2.23694f) + " miles/h ");
            }
        }
    }
    public boolean useMetricUnits(){
        return switchCompat.isChecked();
    }
    @Override
    public void onRequestPermissionsResult(int reqiestCode, @NonNull String[] permissions,@NonNull int[] grantREsults){
        if(reqiestCode == 1000){
            if(grantREsults[0] == PackageManager.PERMISSION_GRANTED){
                goNext();
            }else {
                finish();
            }
        }
    }
}

0 个答案:

没有答案