I have read many posts but cannot find an answer to my specific question.
The service below works well. I can start it. It starts. I can stop it. It stops. I can restart it with a new interval. This works too. When I then stop it, it will not stop. In onDestroy this is the timer = null case. If I insert timer.cancel in onDestroy within the timer = null case, I then get a crash for acting on a null pointer. Yet, it is still running as illustrated by the log output!
Here is the log output if helpful (after the last "noOutRows" it just keeps running)
E/ timer: startservice
E/ timer: timeInterval 1000
E/ timer: isNotNull
E/ timer: no noOutRows 1503945704743
E/ timer: no noOutRows 1503945705712
E/ timer: no noOutRows 1503945706714
E/ timer: no noOutRows 1503945707713
E/ timer: no noOutRows 1503945708712
E/ timer: stopservice
E/ timer: stop - isNotNull
E/ timer: startservice
E/ timer: timeInterval 5000
E/ timer: isNull
E/ timer: no noOutRows 1503945744406
E/ timer: no noOutRows 1503945749407
E/ timer: no noOutRows 1503945754407
E/ timer: stopservice
E/ timer: stop - isNull
E/ timer: no noOutRows 1503945759407
E/ timer: no noOutRows 1503945764407
E/ timer: no noOutRows 1503945769773
Here is my code
public class LocalService extends Service
{
public Integer timeInterval = 1000;
private static Timer timer = new Timer();
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(" timer"," startservice");
int newTimeInterval = intent.getIntExtra("time_interval", -99);
timeInterval = newTimeInterval;
Log.e(" timer ", " timeInterval " + timeInterval);
if (timer == null){
Log.e(" timer"," isNull");
Timer timer = new Timer();
timer.scheduleAtFixedRate(new mainTask(), 0, timeInterval);
} else {
Log.e(" timer"," isNotNull");
timer.scheduleAtFixedRate(new mainTask(), 0, timeInterval);
}
return START_STICKY;
}
private class mainTask extends TimerTask
{
public void run()
{
logHandler.sendEmptyMessage(0);
}
}
public void onDestroy()
{
Log.e(" timer"," stopservice");
if (timer != null) {
Log.e(" timer"," stop - isNotNull");
timer.cancel();
timer = null;
} else {
Log.e(" timer"," stop - isNull");
}
super.onDestroy();
}
private final Handler logHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
int maxRows = MainActivity.Cache.alls.size()-1;
if (maxRows > 0) {
Log.e(" timer ", " outRows " + maxRows + " " + MainActivity.Cache.alls.get(maxRows).toString());
} else {
Log.e(" timer ", " no noOutRows " + MainActivity.utcNow().getTime());
}
}
};
}