Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GhostDog98 opened this issue Apr 1, 2022 · 2 comments
Labels

Comments

@GhostDog98
Copy link

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 ocram added the question label Apr 4, 2022
@ocram
Copy link
Contributor

ocram commented Apr 4, 2022

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants