title: how to stop a service/thread when the application exits or from a different activity
body: <p>now i have a server class that i run on a thread from an activity(i.e servActivity).now when i am not interacting with my app in anyway possible (like i have removed it from recent apps etc) the thread should stop which currently is not stopping. So i researched and i found that i should use a bound service. now a bound service i will have to bind it to servActivity and when servActivity is destroyed i have to unbind and stop service but i dont want to do that. i want to stop service when i am not interacting with the app. i also found that maybe i have to extend application class but cannot find the solution to achieve this?Is it advisable to extend the application class?</p>
  
  <p>i want to be able to create a service running on independent thread from a particular activity(ie servActivity) and then be able to interact with the service from any activity and service should be active (<strong>even if the activity in which i started the service i.e-servActivity is destroyed by going to previous activity etc</strong>) through button or whatever until i am not interacting with the app(i have a notification controller which also needs to be closed to stop the interaction)</p>
  
  <p>i have a client class on one device whose object i create again and again if i have to make request but i want to make only one object for server class because it has a while(true) loop so it keeps running so i want to be able to interact with the server from all activities and stop it when i am not interacting with the application</p>
  
  <p>i also found a way in which i can make an abstract class which extends activity and extend that derived class to all the other activities in my app.But how to i bind the service to all the other activities in the class so that i can interact with the service from all the other activities?And how would i know that if all activities and notification controller have been stopped and there is no interaction with user?something like this <a href="https://stackoverflow.com/questions/14332437/how-to-know-our-app-has-gone-to-background-in-android][1]">how to know our app has gone to background in android</a></p>
  
  <p>If there is there any other method please suggest</p>
  
  <p>Please help
  thanks in advance</p>