Всем привет.
Не могу понять где ошибка.
Задача была такая, при нажатии floating button с точкой, запустить сенсор, а при нажатии floating button с крестиком - поставить на паузу.
Сенсор запускается и работает, а на паузу не становится.
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btn_sensor"
android:onClick="onButtonClickSensor" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btn_stop"
android:onClick="onButtonClickStop" />
<TextView
android:id="@+id/textView"
android:text="@string/acceleration"
/>
А вот и MapsActivity:
package com.ngapp.lab7;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnMapLoadedCallback,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapLongClickListener {
private static final int MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 101;
private GoogleMap mMap;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest mLocationRequest;
private LocationCallback locationCallback;
Marker gpsMarker = null;
List<Marker> markerList;
private FloatingActionButton btn1;
private FloatingActionButton btn2;
private TextView txt1;
static public SensorManager mSensorManager;
static List<Sensor> SensorList;
static final public String SENSOR_TYPE = "sensorType";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
markerList = new ArrayList<>();
locationCallback = new
LocationCallback() {
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
if (gpsMarker != null)
gpsMarker.remove();
Location location = locationResult.getLastLocation();
LatLng centerscreen = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(centerscreen, 3));
}
}
};
}
public void onButtonClickSensor(View v) {
super.onResume();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION) != null) {
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
mSensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("Acceleration:\n x: %.4f", event.values[0])).append(String.format(" y: %.4f", event.values[1]));
TextView valueTextView = findViewById(R.id.textView);
valueTextView.setText(stringBuilder.toString());
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void onButtonClickStop(View v) {
if (mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION) != null) {
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapLoadedCallback(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapLongClickListener(this);
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@SuppressLint("MissingPermission")
private void startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(mLocationRequest, locationCallback, null);
}
public void zoomInClick(View v) {
mMap.moveCamera(CameraUpdateFactory.zoomIn());
}
public void zoomOutClick(View v) {
mMap.moveCamera((CameraUpdateFactory.zoomOut()));
}
@Override
public void onMapLoaded() {
Log.i(MapsActivity.class.getSimpleName(), "MapLoaded");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
return;
}
Task<Location> lastLocation = fusedLocationClient.getLastLocation();
createLocationRequest();
startLocationUpdates();
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
private void stopLocationUpdates() {
if (locationCallback != null)
fusedLocationClient.removeLocationUpdates(locationCallback);
}
Подскажите как решить проблему? Решение кажется наверху, но не срабатывает.
в
onButtonClickSensor сенсор регистрируется, и по идее в
onButtonClickStop он должен быть как unregistered, но нет