Android Live Hardness and Latitude Continuously Renewed

Asked 2 years ago, Updated 2 years ago, 124 views

Let me ask you a question about Android gps.

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, MIN_DISTANCE_UPDATE, this);

If there is a change of distance of 1 second or 10m using this code

public void onLocationChanged(Location location)

I googled to find out that this code is running. When I ran it on my phone, it seemed that I reacted a few times and didn't run it anymore.

What I want is to call public void on location changed(location location) at a certain time and distance, but it doesn't work.

For your information, the onLocationChanged method seems to be responding well when flying GPS is installed and moved artificially.

I'm in desperate need of help. I'll attach the sauce.

(With the app on, I want to get a Toast message after a certain period of time.)

Please point out if there is anything I misunderstand!

public class GPS extensions Service implementations Location Listener {
    private final Context mContext;
    int count = 0;
    boolean isGPSEnable = false;

    boolean isNetWorkEnable = false;

    boolean isGetLocation = false;

    Location location;
    double lat;
    double lon;

    private static final long MIN_DISTANCE_UPDATE = 10;
    private static final long MIN_TIME_UPDATE = 1000 * 10 * 1;

    protected LocationManager locationManager;

    public GPS(Context mContext) {
        this.mContext = mContext;
        getLocation();
    }


    @SuppressLint("MissingPermission")
    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);


            isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetWorkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


            if (!isGPSEnable && !isNetWorkEnable) {
            } } else {
                this.isGetLocation = true;
                if (isNetWorkEnable) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, MIN_DISTANCE_UPDATE, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            lat = location.getLatitude();
                            lon = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnable) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, MIN_DISTANCE_UPDATE, this);
                    if (location == null) {

                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                lat = location.getLatitude();
                                lon = location.getLongitude();
                            }
                        }
                    }
                }
            }
        } } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }


    public double getLatitude() {
        if (location != null)
            lat = location.getLatitude();
        return lat;
    }

    public double getLongitude() {
        if (location != null)
            lon = location.getLongitude();
        return lon;
    }

    public boolean isGetLocation() {
        return this.isGetLocation;
    }

    public void stopUsingGPS() {
        if (locationManager != null)
            locationManager.removeUpdates(GPS.this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

        Toast toast = Toast.makeText(mContext, Integer.toString(count), Toast.LENGTH_SHORT);
        toast.show();
        count++;
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
//        //        Toast toast = Toast.makeText(mContext, "2", Toast.LENGTH_SHORT);
//        //        toast.show();
    }

    @Override
    public void onProviderEnabled(String provider) {
//        //        Toast toast = Toast.makeText(mContext, "3", Toast.LENGTH_LONG);
//        //        toast.show();
    }

    @Override
    public void onProviderDisabled(String provider) {
//        //        Toast toast = Toast.makeText(mContext, "4", Toast.LENGTH_LONG);
//        //        toast.show();
    }
}


android gps

2022-09-21 23:09

1 Answers

It's a self-answer.

The locationManager.requestLocationUpdates method is

It was a method that runs when both conditions were met, not a certain time or a certain distance!


2022-09-21 23:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.