N
N
ngapp2020-01-08 19:13:20
Android
ngapp, 2020-01-08 19:13:20

How to start the sensor with one button, and pause with another?

Hello.
I can't figure out where the error is.
The task was this, when you press the floating button with a dot, start the sensor, and when you press the floating button with a cross, pause it.
The sensor starts and works, but does not pause.
5e16008716a67742163818.png

<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"
 />

And here is the 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);
    }

Tell me how to solve the problem? The solution seems to be at the top but doesn't work.
in onButtonClickSensor the sensor is registered, and in theory in onButtonClickStop it should be like unregistered, but no

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
ngapp, 2020-01-09
@hypnogaja

Resolved by public void onPause (View v) and public void onResume(View v)
events on buttons

D
Denis Zagaevsky, 2020-01-08
@zagayevskiy

There is no code in onButtonClickStop that does what you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question