Problem at Hand:
I have to create a Service
which runs continuously. This service monitors 5 apps say 5 android games installed on your phone. This service needs to get the information of:
1. How many times is the game opened and run?
2. For how much time each game has run.
for example: Say If I have this service installed in my app. And I let it run for a month. I need information of this kind on the screen of the app:
Game Number of times the game is run Duration of Game play
Game 1 20 times played for 15 hours in total
Game 2 16 times played for 25 hours in total
..
..
Game 5 10 times played for 12 hours in total
Possible Approach: When an application loads it comes in the memory. Noting the system clocks time while the application starts. And when the application ends or is put in the background noting the time again.
So say if an application is brought to memory at 9:00 pm and exits to background at 9:30 pm that gives us a gameplay time of 30 mins. Next time the application is played the duration will be added to 30 from the previous play stored in some sort of variable and so on. Each time an application is brought into the memory the counter of it being played should increase by one. Hence giving us the number of times an application is played.
Coding:
I have no idea about Service
in Android as I have never really worked on them. Any tutorials related to my problem at hand will be very helpful.
Secondly, if there is another way in which this could be done. I would like to know that as well. I could really use some code snippet for me to start this project.
As you wrote that the task is about monitoring 3-rd party applications, there is no solution other than periodically read a list of processes and detecting foreground process. You need a service for this. Unfortunately, Android does not provide means such as broadcast events for foreground process change.
The task requires a lot of code in fact, at least much more than an ordinary answer could comprise. I'm posting a part of it here, but you should address many nuances left behind the scenes, such as synchronization and persisting information between launches. This is just a skeleton.
First, lets code an application object, which is a good place to register all instance related stuff.
MonitorApp
Then lets draft an activity.
MonitorActivity
The activity launches a background worker service, which does actually monitor processes. You could possibly move the service registration from the activity into the application instance. The service itself is something like this:
MonitorService
The service utilizes a helper class for building process lists.
ProcessList
Finally, the manifest.
AndroidManifest.xml
As you may see, it's already a lot of code. It's partially extracted from a working application, but I made fast changes for your needs, so there may be typos, all imports are skipped, etc. Nevertheless, I hope this helps a bit.
ADDENDUM: Lollipop+
Beware: the latest Android versions broke the abovementioned approach. Here is what the official documentation says about getRunningTasks method and others:
I think this is an overkill and could be done in much more selective and convenient way. Not to mention that this seems too theatrical considering many built-in features from Google with privacy concerns. Anyway, we can do nothing with this.
The only workaround is to implement Android accessibility service (more info here and here) and intercept all actions with applications gaining and losing focus from there. The user should enable the service manually! Your application should somehow direct the user to do so.