Answer the question
In order to leave comments, you need to log in
Android Theory: Deal with BroacastReceiver and LocationListener?
Hello, I have traveled the Internet, there is a lot of material, solutions, but a theoretical and practical question has matured, I hope for the help of the community.
The situation is as follows, I have 2 classes.
The first class (Activity extends FragmentActivity) contains a google-curth, a path is drawn on it according to gps coordinates, and “Receiver extends BroacastReceiver” is also in it.
The second class "GPSTracker extends Service implements LocationListener" listens for coordinate changes and sends to my receiver.
When the first activity is active, the rendering is on, everything works. But if I turn off the application, the service works, continues to collect coordinates, and so on ... It sends them, but I left the application and unregistered my receiver. When I restore the map, it turns out that I draw the path from the last saved point in the database to the current one.
Question (perhaps when writing a question, the answer has matured by itself): How can I save the coordinates, so that when I restore Activiti, draw them. The only thing that just came to mind was to save the coordinates directly from the service to the database and not from Activity. But I haven’t tried it, I don’t know if it’s possible, is it a “good tone”?
Thank you.
Answer the question
In order to leave comments, you need to log in
keeping a service with coordinate updates (especially GPS) in the background when your application is not active is a bad tone, because this drains the battery a lot.
Read this article if you haven't already.
It implements a more tricky algorithm for obtaining coordinates. It always tries to get the latest coordinates in the fastest and most accurate way, while minimizing the load on the battery.
Of course from the service. The service is no worse than the activity, the difference is that the activity can have a visual interface and is active while visible. The service does not have a visual interface and can work in the background.
In your case, the service is running and collecting data, it doesn’t matter if it has an activity or not, it is busy with its task. If you need to store data somewhere, save it to the database, there is nothing wrong with that. When you create an activity, it binds to the service and asks it for the data it has accumulated and displays it to the user. Having received new data, the service reminds the activity that the array with coordinates has been updated.
It is necessary to save data from the service, and not from activation, because. it can be nailed by the system at any time.
In onResume(), the activity should ask for actual data and draw it on the map.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question