gpt4 book ai didi

java - 如何确定用户正在使用某个应用程序(在android中)?

转载 作者:行者123 更新时间:2023-12-02 08:17:20 26 4
gpt4 key购买 nike

我想记录用户每次使用的应用程序。我可以编写一个程序来保存手机上运行的进程,但我想知道用户正在与之交互的是哪个进程。

最佳答案

我不知道这是否是 Chris 在评论中所指的内容,也不知道它的可靠性如何,但您可以迭代 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.

然后,检查每个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.

您可能有一个服务每 30 秒左右轮询一次并记录这些值。

(顺便说一句,您想要查找那些 IMPORTANCE_FOREGROUNDIMPORTANCE_VISIBLE)

关于java - 如何确定用户正在使用某个应用程序(在android中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6101020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com