Detect App Window Changes: Monitor Foreground Apps
In this article
Yes, your app can detect when another app's window changes (such as coming to the foreground or losing focus), but it requires platform-specific system-level APIs and appropriate permissions. This capability is useful for automation tools, accessibility apps, or productivity monitors that need to respond when users switch between applications. However, due to privacy and security restrictions, not all platforms allow unrestricted access to window state changes. You must use official APIs and ensure your app complies with system policies.
What You'll Need
- A development environment set up for your target platform (Windows, macOS, Linux, Android, or iOS)
- Administrative or accessibility permissions, depending on the OS
- Programming knowledge in a language supported by the platform (e.g., C#, Python, Swift, Java, or Kotlin)
- Access to system APIs for window management or accessibility services
- App permissions declared in the manifest or settings (especially on mobile or modern desktop OS)
Step-by-Step
- Choose your target platform (Windows, macOS, Linux, Android, or iOS) because detection methods vary significantly.
- On Windows, use the Win32 API function
SetWinEventHookto subscribe to window events likeEVENT_SYSTEM_FOREGROUND. This detects when the foreground window changes. You must run your app with at least user-level privileges. - In your code, implement a callback function that processes the
WinEventProcevent. Extract the window handle (hWnd) from the event and useGetWindowTextandGetWindowThreadProcessIdto retrieve the window title and owning process. - On macOS, use the
NSWorkspaceclass. Observe theNSWorkspace.didActivateApplicationNotificationnotification to detect when another app becomes active. You can also pollNSWorkspace.shared.frontmostApplicationperiodically if real-time events are insufficient. - On Linux, use X11 or Wayland APIs. For X11, monitor
FocusInandPropertyChangeevents viaXSelectInput. UseXGetInputFocusto get the currently focused window andXFetchNameto get its title. On Wayland, window monitoring is restricted for security; only compositor-authorized clients can access such data, so this method may not work in standard apps. - On Android, use the
UsageStatsManagerAPI. First, request thePACKAGE_USAGE_STATSpermission. This is a system-level permission; users must enable it manually in Settings > Privacy > Permission manager > Usage access. - Poll usage statistics using
queryUsageStatsover a short time interval (e.g., the last minute). Sort the results by last time used and check the most recent app. Changes in the top entry indicate a window or app switch. - On iOS, detecting other app window changes is not possible for third-party apps due to strict sandboxing. Only apps with special entitlements (e.g., system apps or those using Accessibility features for assistive purposes) can monitor app switching, and even then, only limited information is available.
- For cross-platform solutions, consider using Electron or Flutter with native plugins, but be aware that each platform’s restrictions still apply.
- Test your implementation by switching between known apps and verifying your app logs or responds correctly to the change event.
Common Mistakes
- Assuming window detection works the same across all platforms—each OS has different capabilities and restrictions.
- Forgetting to request required permissions, especially on Android where
PACKAGE_USAGE_STATSmust be manually enabled. - Using polling too frequently, which can drain battery or trigger system throttling.
- Trying to access window titles or process names without proper privileges, resulting in empty or denied responses.
- Assuming iOS allows app switch detection—third-party apps cannot monitor this behavior.
Tips
- On Windows, combine
SetWinEventHookwith process enumeration to get detailed app information. - Use event-driven approaches instead of constant polling where possible to improve efficiency.
- On macOS, include a user prompt to guide them to enable accessibility permissions if needed.
- On Android, cache usage stats and compare deltas to reduce redundant checks.
- Log window change events during development to verify accuracy and timing.
Troubleshooting
If your app fails to detect window changes, first verify that required permissions are granted. On Android, go to Settings > Privacy > Permission manager > Usage access and ensure your app is enabled. On Windows, confirm your process is receiving WinEvents—run as administrator if necessary. On macOS, check that your app is listed in System Settings > Privacy & Security > Accessibility. If events are missed, increase the polling interval or verify the event hook is not being unregistered prematurely. If using UsageStatsManager on Android and getting empty results, ensure the time range is correct and the system has collected data. On Linux, confirm you are using the correct display server (X11 vs Wayland) and that your app has access to the X server. If no method works on iOS, understand that this is a system limitation—no workaround exists for standard apps.
Frequently Asked Questions
Can my app detect when a user switches to a specific app?
Yes, on Windows, macOS, and Android, you can detect when a specific app becomes active by checking the process name or package ID from the window event or usage stats.
Is it possible to detect window changes on iOS?
No, third-party apps cannot detect when another app becomes active due to iOS sandboxing and privacy restrictions.
Do I need admin rights to detect window changes?
On Windows, you typically need user-level access; admin rights are not always required unless hooking system-wide events. On macOS and Linux, standard user access is usually sufficient.
Can I get the title of the active window?
Yes, on Windows and X11 Linux, you can retrieve the window title using API calls like GetWindowText or XFetchName. On macOS, use activeWindowTitle from NSWorkspace.
Why doesn’t my Android app see usage stats?
You must manually enable usage access in system settings. Also, some devices limit how frequently stats are updated, so check a longer time window.
Is detecting window changes safe and allowed?
Yes, if you use official APIs and respect user privacy. Always disclose data usage and avoid logging sensitive information.