第一个应用是 Android 的取最顶部 Activity
。
源码:https://github.com/iOSDevLog/1day1app
001.TopActivity.png
AccessibilityService
观察界面跳转AccessibilityEvent
里面取出需要的包名和类名WindowManager
添加移除 View
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
AccessibilityService
,当窗口变化时显示或隐藏 TopActivity
override fun onInterrupt() {
}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
if (showTopActivityEnabled) {
show(event!!.packageName.toString() + "\n" + event.className)
} else {
dismiss()
}
}
TopActivity
override fun onResume() {
super.onResume()
updateServiceStatus()
}
private fun updateServiceStatus() {
val accessibilityManager = getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
val accessibilityServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC)
serviceEnabled = accessibilityServices.any { it.id == packageName + "/.TopActivityAccessibilityService" }
if (serviceEnabled) {
service_textview.setText(R.string.services_on)
fab.setImageResource(R.drawable.ic_stop_black_24dp)
} else {
service_textview.setText(R.string.services_off)
fab.setImageResource(R.drawable.ic_play_arrow_black_24dp)
showTopActivityEnabled = false
}
service_switch.isEnabled = serviceEnabled
service_switch.isChecked = showTopActivityEnabled
}