Hello. I am writing an application for Android , the generalized structure of which is the main Actvity (plus a bunch of child Activity, but not really related to the question) and the class inheriting the service (Service). By pressing a button in chapters. An activity that is inherited from the service starts and performs some background tasks assigned to it.

Two questions:

a) upon closing the Activity , does my service continue to work autonomously?

b) if a) = true, then how can we determine whether the service is running on the system from under the main Activity launched later (again) at some time or not?

    3 answers 3

    Decision:

    ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50); for (int i=0; i<rs.size(); i++) { ActivityManager.RunningServiceInfo rsi = rs.get(i); Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName()); } 

    This piece of code in my Activity will do what I need. In the cycle, we simply compare the class of service being viewed with ours and continue to do so, as conceived by logic.

      a) true

      b) Insert into the onCreate / onDestroy service setter a flag showing the viability of the service. Further, to understand whether the service is alive or not, it is enough to check the value of the flag

        Send the content to the service via startService(intent) and if the service is started, the service will continue. Otherwise, it will start a new service. Those. You do not need to check whether the service is running or not.

        • Thank. As an option. But, you know, you need to immediately determine whether it already works or not and just lock the button (this is important in this case for me), by clicking on which the service is activated. Or offer to intentionally call startService (...) at the very beginning? How then and in this case to determine whether it is already working or has not yet been launched? - zugzug