I have specific case to ping my server every 10-60 minutes (it still depends) but only when app is opened. This feature is created to inform that session is still open where session is defined as period from app open to app close. I don't have to worry about process kill.
What is better to use? AlarmManager or Handler.postDelayed() ?
The targeted platform is android tv so imagine the case is when watching film in context of my app for example.
Personally I first thought to use AlarmManager but I realized it's way more code to produce compared the circumstances. Is handler causing more CPU usage increase ?
AlarmManager
starts your app in the future when it is not running. So I think Handler.postDelayed()
is a more efficient choice if you will only ping the server when the app is open.
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
See AlarmManager.