简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM