Variables in my Service/IntentService are reset on every start
-
30-10-2019 - |
题
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?
没有正确的解决方案