Yes need to use AlarmManager, but I think it would not be a good approach to keep looking database to check how many messages are pending to send and weather it has got the time to send or not.
Defiantly you need to write a background service for that, But do you think its a good approach?
- Will this service keep looking in db all the time? (Too much resource
consuming)
- What period will you set if you want to run service
periodically? (Compromise on time accuracy of sending)
Its AlarmManager which is most appropriate for this. Just initialize a alarm when you are making entry in db right now. Make you alarm to be RTC_WAKEUP, so that it can send message even the phone is powered off. And every thing will be handled by android itself.
Yes can use database still to have record of all the messages sent by you application, pending to sent, sent successfully, failed etc. Means make entry with messages and its status in db, and just update on respective events like update it to successfully sent when alarm triggered.
Long story, but hope this helps...