Send gps location to a server

Asked

Viewed 57 times

1

I have a service to get the user’s location, but I need to pass the latitude and longitude via request to a server, however I tried to make this request within the method onLocationChanged() using Okhttpclient, but unsuccessfully.

Does anyone have any other hint of what I can do in this situation?

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {

                    Context contexto = getApplicationContext();
                    int duracao = Toast.LENGTH_SHORT;com

                    Toast toast = Toast.makeText(contexto, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(), duracao);
                    toast.show();

                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {


                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }


            }

1 answer

1


It is not recommended to request within the method onLocationChanged() because it is called by the main thread, which controls the user interface. So doing it there will cause crashes (the application will stop responding while the request is being made).

What can be done at this point in the application is to create a separate thread to execute the request.

  • as it is a service, I do thread on own service??

  • I didn’t know it was a Service. Perhaps it is better to pass the latitude and longitude to a second Service responsible for making the request.

  • This is new to me, If I can pass information from one service to another, do I do it within a service? I’m trying to give a research, but if you have some interesting content for me to look for, it would really help

  • 1

    https://stackoverflow.com/q/29192767/2241463

  • And if by kindness, I wanted the location passed to that second X service in X time?

  • Then I would have to see the task scheduling feature of Android. I can’t remember why I’m rusty

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.