Acquiring a copy of the help desk

So, the background service works for me. Now, if the user shuts down, the service will still work, right? Now, when the user restarts the application, I want to access the background service and call some methods. How can I access an instance of a background service?

Thanks guys!

+6
source share
3 answers

It worked out of the box for me! However, do not use it if you have alternatives, because public static members are not good unless they are final. You can create a static variable with a public scope in the service.

public static BackgroundService bs; @Override public void onCreate(){ bs=this;} 

Then initialize the variable with 'this', which makes it a reference to the current current service. Use it as a link in your activities at any time.

+6
source

If the service continues to work after exiting your activity, it depends on how you start it. (Read about this in the startService () / bindService () documentation) If your Service is still running, startService () / bindService () calls will connect you to your "old" service.

+1
source

If you want to communicate with the background service. You cannot use the binding mechanism, and about this you can access this network: android evolve

there is an example to learn how to use the background service

Many thanks

0
source

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


All Articles