Today in the technical summary, the way to share knowledge, personal advocating sharing.
Get the package name of the foreground process in the following ways:
1.android API 10-20
Obtained by Getrunningtasks in Activitymanager.
2.android API 21-22 ( part of the Google update package is not upgraded)
Because the system disables the Getrunningtasks interface above API 21.
Get through getrunningappprocesses in Activitymanager
Cons: Possible miscarriage (probability is low)
3.android API 22
Because the system has disabled the Getrunningappprocesses interface above 22.
Combined with/proc/pid/oom_adj values and bg_non_interactive
Cons: Possibility of miscalculation (probability generally)
4.android API and above:
Combined with/proc/pid/oom_score values and bg_non_interactive
Cons: Possible miscarriage (higher probability)
5. Android API 22 and above:
Obtained by Usagestatsmanager's queryevents interface after opening usagestats permissions
Android app lock get foreground process package name method