How do I know when an app was used in Android? Is there any way to tell when an app was being used in an Android phone?

I am specifically interested in the camera and gallery functions. I believe my roommate went through my photos without asking, and I've tried to view the cache for the Gallery app but can't seem to figure out how to do it. 

Is this even on the right track? He had it for a twenty minute interval, and I want to see if the Gallery was accessed during that period of time.
Android allows usage stats to be seen from GUI. Here's how you can see it. (Source, by Kevin Swartz)


In your default dialer, type *#*#4636#*#*. It would open a window named Testing which is a sub-setting of Settings app. 
Go to Usage Statistics.

  

For Lollipop:


Sort the time in Sort by: based on Usage time or  Last time used or  App name.
The order of entries are App, Last time used, and Usage time.

(Click image to enlarge)

  



For Jelly Bean 4.2.1 to Kitkat 4.4.2:


Sort the time in Sort by: based on Usage time or  Launch count or App name.
The order of entries are App, Count, and Usage time.


(Click image to enlarge)

  

The usage time in these Android versions is shown in milliseconds (ms). You may do calculation on your own, or try a conversion tool like this to instantly convert them into minutes.



Don't forget ADB

Alternatively, you can directly jump into the Usage Statistics window using adb. Enter this command:

For Android Lollipop: 

   adb shell am start -n com.android.settings/.UsageStatsActivity


For Android 4.2.1 to 4.4.2:

   adb shell am start -n com.android.settings/.UsageStats


See this answer if you're interested in knowing the role of parameters in those commands.

You can also do adb shell dumpsys usagestats (Source, by Jinghao Shi) which for Android 4.2.1 and 4.4.2 didn't give anything more useful than already covered in  screenshots posted above for them.

Lollipop shows more end-user comprehensible output in comparison to its predecessors,

In-memory daily stats
  timeRange="8/29/2015, 8:34 AM â 8/30/2015, 1:41 AM" 
    packages
      package=com.android.documentsui totalTime="00:07" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.browser totalTime="00:02" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.camera2 totalTime="00:05" lastTime="8/30/2015, 1:37 AM" 
...
...
events
      time="8/30/2015, 1:34 AM" type=MOVE_TO_BACKGROUND package=com.android.deskclock class=com.android.deskclock.DeskClock 
      time="8/30/2015, 1:34 AM" type=MOVE_TO_FOREGROUND package=com.android.dialer class=com.android.incallui.InCallActivity 
      time="8/30/2015, 1:34 AM" type=MOVE_TO_FOREGROUND package=com.android.dialer class=com.android.dialer.DialtactsActivity 
...
...
In-memory weekly stats
  timeRange="8/27/2015, 5:30 AM â 8/30/2015, 1:41 AM" 
    packages
      package=com.android.documentsui totalTime="00:07" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.browser totalTime="00:02" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.camera2 totalTime="00:05" lastTime="8/30/2015, 1:37 AM"
...
...
In-memory monthly stats
  timeRange="8/9/2015, 8:34 AM â 8/30/2015, 1:41 AM" 
    packages
      package=com.android.documentsui totalTime="00:07" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.browser totalTime="00:02" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.camera2 totalTime="00:05" lastTime="8/30/2015, 1:37 AM"
...
...
In-memory yearly stats
  timeRange="12/21/2014, 5:30 AM â 8/30/2015, 1:41 AM" 
    packages
      package=com.android.documentsui totalTime="00:07" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.browser totalTime="00:02" lastTime="8/30/2015, 1:35 AM" 
      package=com.android.camera2 totalTime="00:05" lastTime="8/30/2015, 1:37 AM"

Now you can browse the list to find Camera and Gallery app, and see their "last time used" value, for Lollipop, that is. Unfortunately, the info in this section wouldn't help the devices running predecessor of Lollipop, unless the device is rooted, or you find an app that can give you package statistics.

Also, in your particular case, all of this info would be helpful only if you've had not launched the Camera, Gallery, or a particular app of your interest after your friend was done using the phone, since the time-stamp for that app gets updated to the new "Last time used", for all Android versions I mentioned.



The following section would work if the device is rooted.



For Jelly Bean (4.2.1) to Kitkat (4.4.2)

Android keeps a log of when  an app (it's component) was last used. You can head down to /data/system/usagestats/ using a file explorer with root access, or using adb. 

There would be a file named usage-history.xml and some other files with naming convention as usage-YYMMDD where:


YYYY - Year 
MM - Month 
DD - Date 


E.g. usage-20150729 is the file created on July 29, 2015.

That .xml is a plain XML file, so you should be able to view it in a  text editor. Those usage-YYMMDD are data files (charset=binary) and my text editor Kate couldn't open them in human-readable form. However, cat &lt;FILE&gt; command does the job, so as the Android app QuickEdit, not completely I should add.

The basic template of entries in that .xml file is:

&lt;pkg name=""&gt;
  &lt;comp name="" /&gt;
  &lt;comp name="" /&gt;
  ...
  &lt;comp name="" /&gt;
&lt;/pkg&gt;


E.g. These are the following entries for the AFWall+ app:

&lt;pkg name="dev.ukanth.ufirewall"&gt;
  &lt;comp name="dev.ukanth.ufirewall.MainActivity" lrt="1438134244890" /&gt;
  &lt;comp name="dev.ukanth.ufirewall.widget.ToggleWidgetOldActivity" lrt="1435337932669" /&gt;
  &lt;comp name="dev.ukanth.ufirewall.HelpActivity" lrt="1416601650972" /&gt;
  ...
&lt;/pkg&gt;


The lrt against every component (comp) is the Unix time. To make it human readable, use Epoch Converter or anything that can convert Unix time into human readable time.

E.g. 1438134244890 is the Unix time when I accessed the AFWall+'s main activity (it's default view) and the converted time is Wed, 29 Jul 2015 01:44:04 GMT, which is correct.

As for the content in usage-YYMMDD, you'll see activities of the apps you visited on that day. There is no time-stamp there.

For Lollipop

Lollipop has considerable changes in regard to usage statistics collection of an app. See official note by  Google here.

The concept of  usage-history.xml and usage-YYMMDD doesn't apply here. Instead, you get directories named daily, monthly, weekly, yearly. All of them has files which you guessed it right, having numeral names, nothing but Unix Time like 1440374400000.

A typical file under daily has the content:

&lt;?xml version='1.0' encoding='utf-8' standalone='yes' ?&gt;
&lt;usagestats version="1" endTime="66355190"&gt;
    &lt;packages&gt;
        &lt;package lastTimeActive="66153798" package="com.android.providers.media" timeActive="2974" lastEvent="2" /&gt;
        &lt;package lastTimeActive="66329769" package="net.dinglisch.android.taskerm" timeActive="64172" lastEvent="2" /&gt;
        &lt;package lastTimeActive="66150806" package="android" timeActive="1942" lastEvent="2" /&gt;
...
...
    &lt;event-log&gt;
        &lt;event time="43080692" package="android" type="5" fs="1065353216" mcc="404" mnc="97" /&gt;
        &lt;event time="43087204" package="com.android.launcher3" class="com.android.launcher3.Launcher" type="2" /&gt;
        &lt;event time="43087329" package="com.android.settings" class="com.android.settings.Settings" type="1" /&gt;


Now this is actually the same thing that we saw in usagestat's  output but the timing is in its raw form. I've no idea how to understand the time here let alone convert it into human-readable form. Developers should be able to do it.

If you what it is and how to convert (as an end-user) then please let me know it or post an answer here.