Question

I'm developing an Android app that needs to do some updating in the background every hour or so. I have a background service which I've made Sticky. And I'm using Timer.scheduleAtFixedRate to schedule the updates.

This seems to work fine. But I've noticed that when I close the app, the next time the scheduled update runs, it causes Application.onCreate to get called again.

This is a problem because Application.onCreate is where I'm grabbing data down from APIs ready to display to the user. I don't want this to happen in the background.

Is this expected behaviour? If so, perhaps I need to add a check in onCreate to see if the app is in the foreground first? Or maybe I've got something set up wrong?

Thanks!

p.s. It's a Galaxy Samsung running Jelly Bean 4.2.1

Background Service code:

@EService
public class BackgroundService extends Service {

    ...

    private Timer timer = new Timer();

    private void performUpdate() {

        // Do the stuff here that we need to do on a schedule...
        Log.i(LOG_CONTEXT, "Perform scheduled update");

        ...            

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

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

        Log.d(LOG_CONTEXT, "Background thread started");

        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                performUpdate();
            }
        }, 0, UPDATE_INTERVAL);

        // Sticky means service will continue running until explicitly stopped
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        Log.d(LOG_CONTEXT, "Background thread stopped");            
        timer.cancel();

    }

}

Application code:

@EApplication
public class MyApplication extends Application {

    ...

    @Override
    public void onCreate() {
        super.onCreate();
        initApp();
    }

    private void initApp() {

        // This is where I want to do stuff when the app is actually
        // opened by the user, not every time the background service
        // update occurs!

        Log.i(LOG_CONTEXT, "Initialise. Why does this happen again after app's closed?");

        ...

    }

    ...

Log:

12-09 16:28:15.828: I/MyApplication(3049): Initialise. Why does this happen again after app's closed?

[Now I close the app, by pressing the Recent Apps menu button and swiping it away]

12-09 16:28:16.015: I/BackgroundService(3049): Perform scheduled update
12-09 16:28:33.875: I/MyApplication(3080): Initialise. Why does this happen again after app's closed?
Was it helpful?

Solution

Your service runs as a part of your application, so the application is created for it.

Most apps do not need to extend Application. Without seeing all of your code, I'm pretty sure you don't need to either. Just extend Activity for the class that displays stuff to the user and do the API stuff in that. That will not be created when the service runs.

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