ibaguio
ibaguio

Reputation: 2368

When to use Android AlarmManager?

I havent had the time to dig deeper on AlarmManager, and how it really works on the low level. I am concerned about the efficiency, expense, and any alternatives to this API. Can anybody shed some light on the matter. Specifically is it efficient to have set AlarmManager to run a IntentService EVERY few SECONDS (atleast 2-3 seconds)? Or is it better to use a Service, and create a different thread that has a loop and pauses every few seconds? Or is there any other alternatives to this?

It would also be great if anyone can share some insights about Power, and Memory consumption comparison of using AlarmManager or Service, or any other method.

I am using the AlarmManager to call my IntentService every few seconds to run a piece of code that checks if there are new Files in a target folder.

Upvotes: 1

Views: 201

Answers (1)

kennytm
kennytm

Reputation: 523484

You should not AlarmManager or even IntentService to check if there are new files in a target folder. Android has the FileObserver class to check this without a busy loop.

FileObserver observer = new FileObserver("path/to/target", FileObserver.CREATE) {
    @Override
    public void onEvent(int event, String filename) {
        if (event == CREATE) {
            Log.i("Info", filename + " has been added to folder");
        }
    }
};
observer.startWatching();

Upvotes: 1

Related Questions