Question

I declared a service, that shall act as a queue. Therefore I have a variable that tells the service it's the first start and another one that stores a value.

The code looks like this:

public class TTSQueue extends Service {

private Integer lastvol = 0;
private Boolean isFirstStart = true;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle b = intent.getExtras();
    Integer vol = b.getInt(TTS.PARAM_LAST_VOL, 0);
    if (vol > lastvol) {
        lastvol = vol;
    }
    if (isFirstStart) {
        isFirstStart = false;
        startAnotherService();
    } else {
        waitForAnEventAndThen_startAnotherService();
    }

}

Okay, the problem is, this Service is started from a receiver. And everytime it is started, both lastvol and isFirstStart are reset. I thought, if the Service is already created and then started with an Intent, it would only call onStartCommand() again and not reset everything.

I also tried only declaring the variables and setting them to the default value in onCreate(), but that had the same effect. Also I tried replacing the Service with an IntentService, but that doesn't help either, same problem.

I would like to avoid using SharedPreferences, as I don't think it is necessarily needed in this case. (And I don't want to waste the user's Write Cycles).

Am I missing something? I guess it is not because of the private declaration, is it?

Was it helpful?

Solution

This surely does not have anything to do with members being private.

I'm not sure what's the problem, but I could imagine one of these candidates:

  • The service terminates before a second request arrives.
  • The Receiver spawns a new process because it responds to a "system intent".

You could evaluate this guesswork by letting your service log something periodically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top