单击一个切换按钮时,是否可以获取GPS位置以确定当前速度?

时间:2019-04-27 09:41:49

标签: java android gps location

我正在构建一个应用程序,以在驾驶时尽量减少手机的使用。为此,我需要知道用户何时以一定速度行驶时,有人可以帮我吗?

我已经获得了设备的当前位置,但是我不知道如何更改它以获取速度

public class MainActivity extends AppCompatActivity {

//Constants
final int REQUEST_CODE = 123;
//time between location updates (1 seconds)
final long MIN_TIME = 1000;
//distance between location updates (10 meters)
final float MIN_DISTANCE = 10;


// GET location
String LOCATION_PROVIDER = LocationManager.GPS_PROVIDER;

//declare LOCATION MANAGER and LOCATION LISTENER
LocationManager mLocationManager;
LocationListener mLocationListener;

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

    ///////////////////MAIN TOOGLE BUTTON/////////////////////
    ToggleButton toggle = findViewById(R.id.mainbtn);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Toast.makeText(getApplicationContext(),"FirstLine Running...", Toast.LENGTH_LONG).show();

            } else {
                // The toggle is disabled
            }
        }
    });

    //Linking elements to Java code
    ImageButton cog_wheel = findViewById(R.id.cog_wheel);

    //onClick listener
    cog_wheel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(MainActivity.this, fl_settings.class);
            startActivity(myIntent);
        }
    });

}

//On Resume
//@Override

@Override
protected void onPostResume() {
    super.onPostResume();
    Log.d("FL", "OnResume called");
    Log.d("FT", "Get Current Location");
    getCurrentLocation();
}

// getting the Current Location
private void getCurrentLocation() {

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            Log.d("FL", "onLocationChanged() callback received");
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

            Log.d("FL", "onProviderDisabled() callback received");
        }
    };


    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.
        ActivityCompat.requestPermissions(this,
                new String [] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_CODE){
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Log.d("FL","onPermissionResults(): Permission Granted!");
            getCurrentLocation();
        } else{
            Log.d("FL","Permission denied =( ");
          }
      }
  }
}

0 个答案:

没有答案