Android final in Service and update interface

I have a little question about threads and services. I have an example that downloads data over the Internet in a stream that runs in a service. What I want to know is how I can determine when my stream is completed (all data is loaded), and then call the onDestroy() , which will update the interface in all the actions I have to do.

Any suggestions on how to do this, and this is the right way that I should do this.

Thanks in advance!

+4
source share
3 answers
  • Finished download detection: If you are using a simple Thread and not AsyncTask , you need to create a callback called by the thread to notify the service of completed downloads (e.g. onDownloadsFinished() ).
  • Communicating with Activities: There are many ways to interact between services and activities, but I find the binding to the service the simplest. When bound to a service, Activity can register callbacks directly to the service.
  • Change user interface: Make sure you use runOnUiThread when trying to change the user interface from a callback, because it will run async.
  • Stopping the service: While the action is tied to the Service, it will start. This is not a problem, because when there is no thread in doing something, it is unlikely to consume any resources. You only need to call stopSelf() inside onDownloadsFinished() if you use startService() to start loading the background. See the service documentation for more information.
+4
source

1) use can use the messenger to communicate between the activity and the service. 2) if your data is stored in any file or database, then just send a broadcast receiver that tells you its time to update. 3) use the interface. but in this case you will need to save an object of this interface that even a service can use.

0
source

Firstly, it depends on which web client you use to load data, this client determines the end of incoming data, after which you must call the stopSelf() method in the Service object class, than define your actions in the onDestroy() method block

0
source

Source: https://habr.com/ru/post/1383819/


All Articles