Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 513
Ishwar Avatar asked Apr 28 '12 10:04

Ishwar


2 Answers

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.

like image 166
rahulserver Avatar answered Nov 20 '22 01:11

rahulserver


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.

like image 1
Christian S. Avatar answered Nov 20 '22 02:11

Christian S.