onStartCommand() called only once even when Service is started multiple times

1.3k Views Asked by At

The below service is triggered via button click from some other app (by firing pending Intent). The onStartCommand() creates a Messages and dispatches using send() method. Ideally, I expect onStartCommand to be called everytime button is clicked, as a pending intent is used to fire the service on buttonClick.

But onstartCommand() is called only once, for the first time the button is clicked. Subsequent button clicks do not trigger the onStartCommand().

Interestingly if I comment the line replyTo.send(msg); onStartCommand gets called each time the button from other app is clicked.

Therefore dispatching the Message using android IPC Messenger from within the service might be causing the issue. I confirmed the Message reaches the destination app successfully. Am I missing some detail about Messages , like blocking send call?

I am returning 'START_STICKY' from onStartCommand(), that also might be the reason.

Any insights on what is happening will be welcome.

//MyService.java

@Override
public void onCreate() {
    // create RemoteViews -> rView
    Intent intent = new Intent(getBaseContext(), MyService.class);
    PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), 0, intent, 0); 
    rView.setOnClickPendingIntent(buttonId, pendingIntent);
    //On click of the above button, this MyService will be started usingthe given pendingintent
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("debug","Service onStartCommand");

    Message msg = Message.obtain(null, UPDATE_REMOTE_VIEW, rView);

    try {
        replyTo.send(msg);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return START_STICKY;
}

Bonus Detail: The pendingIntent on the Button (from other app) is set using setOnclickPendingIntent() (RemoteViews class).

2

There are 2 best solutions below

0
On

What I did in my similar case is to implement onStartCommand as follow:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //
    // ... HERE support for intent sent by setOnClickPendingIntent ...
    //

    return super.onStartCommand(intent, flags, startId);
}

And it seems to work. onStartCommand is called multiple times (as many as the number of click on my RemoteViews).

3
On

From Docs:

Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.

So, it may be because of the use of bindService