Ishwar
Ishwar

Reputation: 175

Acquiring instance of Background Service

So i have a background service running. Now, if the user quits the activity, the service will still be running, right? Now when user restarts the app, I want to access the background service and call some methods. How can I access the instance of the background service ?

Thanks Guys!

Upvotes: 6

Views: 9323

Answers (3)

sarah
sarah

Reputation: 1

If you want to communicate with the background service. You can't use the binder mechanism, and about this you could access this web :android develop

there is a example to tell to how to use the background service

thanks a lot

Upvotes: 0

rahulserver
rahulserver

Reputation: 11205

This worked for me out of the box!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 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 running service. Use it as a reference in your activity anytime.

Upvotes: 6

Christian S.
Christian S.

Reputation: 321

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

Upvotes: 1

Related Questions