delight-im / Android-SimpleLocation

Utility class for easy access to the device location on Android
Apache License 2.0
200 stars 75 forks source link

Only Updating very occasionally, despite update interval being set to 500 #41

Open GhostDog98 opened 2 years ago

GhostDog98 commented 2 years ago

Heyo, I'm having an issue where the GPS location is only being updated very occasionally, despite the fact that I have explicitly defined it as 0.5s (500ms).

My Code:

public class MainActivity extends AppCompatActivity {

    public SimpleLocation location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Context context = this;
        boolean requireFine = false;
        boolean passiveMode = false;
        long updateInterval = 500;
        boolean requireNew = true;
        new SimpleLocation(context, requireFine, passiveMode, updateInterval, requireNew);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = new SimpleLocation(this);  // Initialise

        if (!location.hasLocationEnabled()) {
            // ask the user to enable location access
            SimpleLocation.openSettings(this); // Open settings if the user hasn't put in location permissions.
        }

        location.beginUpdates();
        TextView main = (TextView)  findViewById(R.id.multitext);
        main.setText("Epoch_ms, lat, long\n");

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
        @Override
        public void run() {  // Loop forever with 1000ms delay
            handler.postDelayed(this, 1000);

            SimpleLocation.Point position = location.getPosition();
            Log.d("Debug", "Running run");
            main.append(((System.currentTimeMillis()) + "," + String.valueOf(position) + ";" + "\n"));
        }},1000);

    }
}

I'd expect it to output a different value every time it loops (as it should have updated at least twice by then) but it never seems to. It only prints the same value over and over again, however, it is a valid value. I have tried this both in my house, running, as well as in a car, with the same results. It seems if I leave it for a bit, then open the app, it's happy to grab my location, but doesn't update it as I move... I'm testing it on an OPPO Reno Z (CPH1979) on ColorOS 11.1, and I've confirmed the app has all of the permissions granted, and have tried with both Course and fine, and both with requireNew=true and false.

Many thanks, GhostDog98

ocram commented 2 years ago

Thanks!

Do you have these two implemented?

    @Override
    protected void onResume() {
        super.onResume();

        // make the device update its location
        location.beginUpdates();

        // ...
    }

    @Override
    protected void onPause() {
        // stop location updates (saves battery)
        location.endUpdates();

        // ...

        super.onPause();
    }

And, instead of the loop, could you try the following?

location.setListener(new SimpleLocation.Listener() {

    public void onPositionChanged() {
        // new location data has been received and can be accessed
    }

});
GhostDog98 commented 2 years ago

Hi, thanks for the help. Still not working as it appears the "onPositionChanged" never gets called, unless if I re-open the app or turn off then re open my phone (maybe this is due to it calling "beginUpdates()"? This only happens once though, and still seems to not be updating location.

For reference, this is my current code now:

public class MainActivity extends AppCompatActivity {

        public SimpleLocation location;
    @Override
    protected void onResume() {
        super.onResume();

        // make the device update its location
        location.beginUpdates();

    }

    @Override
    protected void onPause() {
        // stop location updates (saves battery)
        location.endUpdates();

        super.onPause();
    }

        @Override
        protected void onCreate (Bundle savedInstanceState){
        Context context = this;
        boolean requireFine = false;
        boolean passiveMode = false;
        long updateInterval = 1000;
        boolean requireNew = true;
        new SimpleLocation(context, requireFine, passiveMode, updateInterval, requireNew);

        // Request permissions
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = new SimpleLocation(this);  // Initialise
        if (!location.hasLocationEnabled()) {
            // ask the user to enable location access
            SimpleLocation.openSettings(this); // Open settings if the user hasn't put in location permissions.
        }

        TextView main = (TextView) findViewById(R.id.multitext);
        main.setText("Epoch_ms, lat, long\n");
            location.setListener(() -> {
                SimpleLocation.Point position = location.getPosition();
                Log.d("Debug", "Running run");
                main.append(((System.currentTimeMillis()) + "," + position + ";" + "\n"));
            });
    }

}

Am I maybe implementing the new simplelocation in the wrong place? Many thanks for your continued help, GhostDog98