简体   繁体   中英

How to fetch current longitude and latitude in android programaticaly at every secon?

I want to make app like tracker through the help of that admin can track specific user with his permisiion at specific time, so i required to track users long and lat at every second and pass to somewhere else.

You could write a recursive method that makes a network call to send the user location and keeps calling itself anytime you get a successful callback, something all this line;

    private void sendLocation(final String long, final String lat){
    repository.getLocationApiService().sendLocation(long, lat)
            .enqueue(new Callback<SendLocationResponse>() {
                @Override
                public void onResponse(Call<SendLocationResponse> 
                    call,Response<SendLocationResponse> response) {
                    if(response.isSuccessful()){
                        sendLocation(long, lat);
                    }
                    else{
                        //Handle error
                    }
                }

                @Override
                public void onFailure(Call<SendLocationResponse> call, **strong text**Throwable  t) {
                    //Handle networking errors
                }
            });
} 

This way the next location is sent immediately the previous has gone through, I hope this helps, also was making use of retrofit in the method.

Asking GPS location every second might be pretty expensive in terms of battery performance for example. Also, is there a need to update location that frequently? I would consider if it is really necessary. And there might be some hardware limitations on some of the devices that will not allow location requests at such small interval.

Other than that, use Timer to run your function on the selected interval.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM