简体   繁体   English

如何确定用户正在使用的应用程序(在android中)?

[英]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 () :我不知道这是否是 Chris 在评论中所指的,我不知道它有多可靠,但您可以遍历ActivityManager#getRunningAppProcesses ()返回的列表:

 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses ()

Since: API Level 3自:API 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).返回RunningAppProcessInfo记录列表,如果没有正在运行的进程,则返回 null(它不会返回空列表)。 This list ordering is not specified.此列表排序未指定。

Then, check for the importance field of each ActivityManager.RunningAppProcessInfo :然后,检查每个ActivityManager.RunningAppProcessInfo的重要性字段:

 public int importance

Since: API Level 3自:API 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 .可能是IMPORTANCE_FOREGROUNDIMPORTANCE_VISIBLEIMPORTANCE_SERVICEIMPORTANCE_BACKGROUNDIMPORTANCE_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.您可能每 30 秒左右轮询一次服务并记录这些值。

(BTW, you want to look for those IMPORTANCE_FOREGROUND and IMPORTANCE_VISIBLE ) (顺便说一句,你想寻找那些IMPORTANCE_FOREGROUNDIMPORTANCE_VISIBLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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