开发者

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. 开发者_C百科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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜