2

Hello I am developing an application where I required to find time spent by user on a certain app like Facebook.

Whenever user has spent x minutes then I want to give some reward to user. Is there any good approach to achieve this in Android ?

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • You mean you need to mesure the time spent on **another** application that the one your will be building? – shkschneider Dec 17 '14 at 15:10
  • @shkschneider Yes I want to measure how much user spent time on another apps. If user finish x minutes then I want to give some credit to user. – N Sharma Dec 17 '14 at 15:15
  • You can try reading the log files, http://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched – acid_srvnn Dec 17 '14 at 15:24
  • 1
    @AcidBurn Unfortunately, this has been disallowed since jellybean. (For non-system apps) – Steven Mann Dec 17 '14 at 15:30

2 Answers2

2

As noted in the question Detect When other Application opened or Launched, you cannot listen for other applications launch signals, but you could sample the list of open applications every so often to check if the app you are looking for is a) running and b) in the foreground.

You would have to run this application as a service in the background, and I'm pretty sure that it could have devastating implications on the user's phone battery life.

Community
  • 1
  • 1
Steven Mann
  • 558
  • 3
  • 14
  • If you see applock app https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en then how they guys do because it doesn't impact much on battery life. – N Sharma Dec 17 '14 at 15:20
  • Emphasize on *could*. It is possible to write the sampler so that it sleeps between updates and preserves battery life: it was more of a "be careful", not a "you can't do this!" ;) – Steven Mann Dec 17 '14 at 15:22
  • Well He can listen if the phone screen is on and then doing a sample on open applications. If he don't check every millisecond, I don't think it will be devastating on battery life. – Vincent D. Dec 17 '14 at 15:53
  • @Williams This looks like a very reasonable approach. – matiash Dec 22 '14 at 00:29
  • @matiash Yup. Looks like a nice idea – N Sharma Dec 23 '14 at 19:51
0

You'll need a background Service that will check the phone's activity (applications in foreground) and count their active/foreground elapsed time.

List foreground applications

 <uses-permission android:name="android.permission.GET_TASKS" />

You can detect currently foreground application with ActivityManager.getRunningAppProcesses(). It will return a list of RunningAppProcessInfo records.

To determine which application is on foreground check RunningAppProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND.

How much time has been spent on it

Process.getElapsedCpuTime() is the best to go IMHO.

Unfortunately I don't know how to get the Process of another application...

So you could have a looping Service incrementing spent time everytime an application is on foreground, but that would not be super precise I'm afraid.

Another way would be to control yourself the opening of those apps and when they leave it and come back to yours, evaluate the spent time in foreground at that moment.

Just some thoughts, I don't have a complete answer, sorry.

shkschneider
  • 17,833
  • 13
  • 59
  • 112