You can use AccessibilityService to get event notification. But AccessibilityService are specifically for accessibility uses. If you use the service for other purposes, then the application will more likely to be downed/removed for PlayStore due to policies. Another viable option is UsageStatsManager, but with some limitations.
- UsageStatsManager is not push event based system. You have to poll in few mills(depends upon the use-case)
- Usage Access Permission grant/deny is not straight forward. You have to start Setting Activity with Settings#ACTION_USAGE_ACCESS_SETTINGS action and have to rely on the users understanding of how to grant permission(Since permission list may contain other applications).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…