I am working on a program which consists of two parts
Service component will read various system features (CPU usage, RAM usage, Number of Running Tasks, Messages Sent, Calls Made etc) every pre determined time interval lets say 15 minutes. And save these readings/data in a database.
Activity component will read the data from SQL database file and process the information.
My Questions are
-
What kind of service do I have to create that will stay alive forever until stopped by the activity it should also automatically restart after system reboot?
-
Has anyone got an example of service writing data to database?
-
Can I invoke the parent activity from the service?
Sounds like a fairly standard
Service. Read up on theServicelifecycle.Answers:
BroadcastReceiverthat listens for theBOOT_COMPLETEDintent to start the service at boot.Serviceobjects areContextobjects, so you can do anything with a SQLite database that you could do from anActivity. No difference.Activityfrom the service via the standardstartActivity()method fromContext. If you start theServicefrom theBroadcastReceiverat boot, it’s an independent service not connected to anyActivityso there is no parentActivity.Note also that a
Servicemay not be absolutely necessary for your stated intent. If you’re only doing things that infrequently, you may be able to get by with anAlarmManageralarm. That way you’re not leaving aServicerunning — and consuming resources — for something you’re only processing every 15 minutes.