Measure Time Spent on Android Applications -


i new android. in application, want track how time other applications (which installed on device) used (in foreground).

is possible? if yes how?

thanks in advance!

first thing , that's required known here applications running in foreground :

you can detect foreground/background application activitymanager.getrunningappprocesses() call.

so, ::

  class findforegroundprocesses extends asynctask<context, void, boolean> {        @override       protected boolean doinbackground(context... params) {         final context context = params[0].getapplicationcontext();         return isapponforeground(context);       }        private boolean isapponforeground(context context) {         activitymanager activitymanager = (activitymanager) context.getsystemservice(context.activity_service);         list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses();         if (appprocesses == null) {           return false;         }         final string packagename = context.getpackagename();         (runningappprocessinfo appprocess : appprocesses) {           if (appprocess.importance == runningappprocessinfo.importance_foreground && appprocess.processname.equals(packagename)) {             return true;           }         }         return false;       }     }      //  call like:     boolean foreground = new findforegroundprocesses().execute(context).get(); 

you can check out : determining foreground/background processes.

to measure time taken process run due course , can use method :

getelapsedcputime()   

refer article .


Comments