I implemented Service and I need to call another thread from it ...

The fact is that I have a class that is used in several places and it starts a thread when it is executed, and now I need to make it so that this class runs in the background ...

I did what I did: I would implement the service and I want it to run my class in my background in which the thread is also implemented.

And I'm thinking, the service will start my class, my class will start its stream, the service does not know that the stream started there and will think that the work is finished and will die ...

Is it so?

And if so, how can you make him wait?

  • The service itself will not die. - Vladyslav Matviienko
  • @metalurgus means there is another problem, then I’ll have to show the service how to show that everything is already done and kill it ... How to do it? - Aleksey Timoshchenko
  • You just need to bind the service, to this activity where the task takes place. those bindActivity, and implement the logic already in the activation methods. Since your service needs to be closed after completing the task, this is a good option, moreover you will have CallBack methods in which you can implement direct feedback to the Service. - Shwarz Andrei
  • one
    @ShwarzAndrei but in the end I did just that - Aleksey Timoshchenko

1 answer 1

Service will live until the system crashes it, until the application is nailed by the user via FORCE STOP or until you stop it yourself like this:

 context.stopService(new Intent(context,SERVICE_CLASS_NAME.class)); 

IntentService, by default, does not operate in the UI stream, unlike Service. And he will die when he finishes his work.

  • I did not fully understand about the IntentService ... as far as I understand the service in the printer chain does not work in the UI stream, since it works in the back ... All I need to output to the UI I do with runOnUiThread() ... - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, just try to access the network from the service without wrapping the request into another stream - NetworkOnMainThreadExeption should pop up - YuriySPb