Question

I have a problem regarding the lifetime of a variable.

My Android-App should write sensor-data to a file and send this file to a server. For this I created a Activity (Main-Activity) and two Services. One is for writing the sensor-data to files (Sensor-Service) and one for sending the files to a server (Upload-Service). The activity has two toggle-buttons for start/stop the two services seperately.

The communication between the two services works with a list: When a new file with sensor-data is ready (has a particular size) the name of the file is appended to a list. The Upload-Service checks in an infinite-loop if new files are in the list and uploads them.

But this list causes a problem: When my App runs in Background and I open many other Apps at some point the reference on the list becomes NULL in Sensor-Service (and probably also in Upload-Service).

The list is defined in the Main-Activity:

private static LinkedBlockingDeque<String> mFileSendQueue;

and initialised in onCreate of the Main-Activity:

mFileSendQueue = new LinkedBlockingDeque<String>();

and passed to the two Services via a static method setQueue (both services contain such a method):

public static void setQueue(LinkedBlockingDeque<String> queue) {
    mFileSendQueue = queue;
}

I know that the Activity can be closed by Android e.g. if space is required, but in my opinion the list shouldn't be deleted as long as there is at least one reference to it. Does anybody know what my mistake is?

Thanks for reading

Was it helpful?

Solution

Id recommend that your "sender service" keeps record itself. You could name files by timestamps and the sender stores the last timestamp that was uploaded.

Android / dalvik vm seems to delete references after some time in the background. I've seen it happen in my services, suddenly hardware sockets are null etc.

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