Today in the technical summary, the way to share knowledge, personal advocating sharing.
Listen to whether the app's front and back switches have occurred in the following ways:
1. Android API 10–15
The Activitymanager Registeractivitywatcher method registers the Iactivitywatcher mode of monitoring.
2. Android API 16–20
Listen for changes in application switching by listening for changes in/dev/log/events file content.
Why this approach can be successfully monitored:
Because/dev/log/events is the kernel of the log output character device terminal file.
By looking at the Logger.h (Linux kernel log output System) Discovery defines:
Define/dev/log/events log output device files
by viewing Logcat.cpp Source We will find:
His readloglines (log_device_t * devices) function
The output of the kernel log is monitored through the select mode and then output to the screen terminal, so we can enter ADB locat in terminal to see the system log output as follows: I click the SMS app
Look at the Readloglines function in log_device_t creation:
Why use this way to listen:
Because the Android api16-20 uses the Iactivitcontorller or iprocessobserver instead of the iactivitywatcher, this approach only listens to its own activity switch, System permissions are required to listen to other applications.
3. Android API and above:
Listen for changes in application switching by listening for changes in Bg_non_interactive/tasks file content.
Why this way can be monitored successfully
This is, of course, a bug left in the design of the system, when the app occurs before and after the switch, his PID will be added or deleted in the Bg_non_interactive/tasks
Why use this way to listen:
Because/dev/log/events can not be in the user-state monitoring, see Logcat Source code did not choose to listen directly, or through the interface provided by the logger to listen to the kernel state
4. Other Auxiliary monitoring methods:
Listen to the corresponding/proc/pid/oom_adj is worth the change to monitor before and after switching
5. fallback mode:
Use polling to get app switching changes (power consumption, temporarily unused)
Android application lock monitoring application before and after the switch mode