User defined method and onStartCommand() in Service class
08:04 21 Nov 2015

I read in online tutorials that services are long lasting objects and these are used for long term tasks. Eg. maps, play music in the background etc. I also read some more stuff on services.

To implement bounded service, I did following:

In the service class's onStartCommand(), I started a thread. Inside the run() method, I added a loop with 10 iterations. Each iteration takes a sleep of 5000 milliseconds and increments currentState (int variable) by 1.

There is a method int showThreadStateToClient(). It returns currentState to the MainActivity.

Following are two code snippets of the Service class. Please tell me what is the difference between them?

Code snippet #1 (with onStartCommand):

public class MyService extends Service {

    IBinder iBinder = new LocalBinder();
    static int currentState = 0;
    int totalIterations = 10;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (currentState <= totalIterations) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ++currentState;
                }
            }
        };

        Thread t = new Thread(runnable);
        t.start();

        return Service.START_STICKY;
    }

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

    public int showThreadStateToClient()
    {
        return currentState;
    }
}

Code snippet #2 (without onStartCommand()):

public class MyService extends Service {

    IBinder iBinder = new LocalBinder();
    static int currentState = 0;
    int totalIterations = 10;

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

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

    /* this method is called form MainActivity after binding the service. */
    public void startThread()
    {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (currentState <= totalIterations) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ++currentState;
                }
            }
        };

        Thread t = new Thread(runnable);
        t.start();
    }

    public int showThreadStateToClient()
    {
        return currentState;
    }
}

Will thread running in code snippet #2 gets all the same rights as the thread running inside onStartCommand() in code snippet #1.

android service