Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Android Local Service instance

I'm starting a service in my application using startService.

I do not want to use bindService as I want to handle the service life time myself.

How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.

Thanks.

/ Henrik

like image 683
Henrik Avatar asked Jun 11 '10 18:06

Henrik


People also ask

How do I find my service instance Android?

Use both startService() and bindService() , if needed. How can I get an instance to the service started if I do not use bindService? Either use bindService() with startService() , or use a singleton. By "using a singleton" you mean that I should declare my methods static in the service class?

How can I tell if an Android service is running?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.

What is the use of onBind () in Android?

To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

How do I keep my service alive Android?

But in order to keep a service alive like playing a song in a background. You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section. In this way the app will keep alive in background without any interuption.


2 Answers

I do not want to use bindService as I want to handle the service life time myself.

That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.

How can I get an instance to the service started if I do not use bindService?

Either use bindService() with startService(), or use a singleton.

like image 64
CommonsWare Avatar answered Oct 16 '22 11:10

CommonsWare


Here's another approach:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {
    private Binder binder;  

    @Override
    public void onCreate() {
        super.onCreate();
        binder = new Binder();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class Binder extends android.os.Binder {
        public MyService getService() {
            return MyService.this;
        }
    }
}

onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.

like image 37
Brodo Fraggins Avatar answered Oct 16 '22 11:10

Brodo Fraggins