Search code examples
androidmultithreadingandroid-serviceandroid-intentservice

Monitoring IntentService with AlarmManager or Service with Thread


I'm building a monitoring app that will capture as much info as possible from the mobile device, like running processes / active connections / networking statistics / active interfaces etc. Obviously I will need a service that will be running in the background for that, but I'm not entirely sure how to implement it.

Someone suggested that I create an IntentService that will execute at specific intervals using AlarmManager, do its thing and then die again. In this thread people suggest an implementation using an always-on Service that starts its own thread to do the work, put it to sleep and then again. One also suggested that AlarmManager is used too to make sure that the service will be restarted if the OS kills it. What's the mpst appropriate implementation for monitoring real time data? (or the up/downsides of each). Note that many of the info I'm capturing do not produce intents (so I can't just register receivers)

Thanks a lot:)


Solution

  • Note that many of the info I'm capturing do not produce intents (so I can't just register receivers)

    Then you won't be able to use an IntentService, since your app won't know when to fire it up.

    If you want "real time" updating of info, then you will have to use a Service (with or without it's own background thread). You cannot use an AlarmManager because it will almost always run too late (not "real time").

    Do note that it takes some effort to have your service run always as there are a couple of different scenarios where it can stop running, and even when you have done all you can to achieve it, there are still ways for OS or user to stop it from running.