Question

I'm writing an App and an accopmanying widget that needs data. This data is downloaded over the net. I'm planning to use a IntentService to do the downloading. This service will save the data as a file. This file is then used by both the App and widget.

How can I prevent the app and widget from reading the file while the service is writing to it?

And how can i prevent the service to write to the file while the app or the widget is reading the file?

Thanks!

Was it helpful?

Solution

Consider extending Application and declaring a field for reading and writing permission:

public class APP extends Application {
    boolean motherMayI;

    ...
}

Then whenever you open a FileInputStream or a FileOutputStream,

...
if (motherMayI) {
    APP.motherMayI = false;
    FileInputStream input = context.openFileInput("some_file_name");

    //read your stuff

    input.close()
    APP.motherMayI = true;
}
...

Essentially, the field motherMayI must be true for someone to read or write the file, and its only false while its being read or written.

EDIT:

public FileInputStream getFileInputStream() {
    return context.openFileInput(file_name);
}

This would require an instance of APP.

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