简体   繁体   中英

How to determine that an application in being used by the user (in android)?

I want to record what application is being used by the user at every time. I can write a program to save the processes running on the phone, but I want to know which one is the one that the user is interacting with.

I don't know if this is what Chris was referring to in the comment, and I don't know how reliable it is, but you can iterate through the list returned by ActivityManager#getRunningAppProcesses () :

 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses ()

Since: API Level 3

Returns a list of application processes that are running on the device.

Returns a list of RunningAppProcessInfo records, or null if there are no running processes (it will not return an empty list). This list ordering is not specified.

Then, check for the importance field of each ActivityManager.RunningAppProcessInfo :

 public int importance

Since: API Level 3

The relative importance level that the system places on this process. May be one of IMPORTANCE_FOREGROUND , IMPORTANCE_VISIBLE , IMPORTANCE_SERVICE , IMPORTANCE_BACKGROUND , or IMPORTANCE_EMPTY . These constants are numbered so that "more important" values are always smaller than "less important" values.

You may have a service polling every 30 seconds or so and log those values.

(BTW, you want to look for those IMPORTANCE_FOREGROUND and IMPORTANCE_VISIBLE )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM