I'm using an accessibility-service to check which application I'm using for-example I am using WhatsApp and I am trying to get its package name and compare it with my database if the package exists in my database then I'm closing the app so that I can't use this app, I just working on my idea. It's been days since I got this problem and I can't find the solutions to my problems can anyone help me. Thank you
The Problems that I am facing are listed below:
- Accessibility service is automatically closing when I try to close my app. If I don't close the app it's working fine. For-example If I close my main application the service will turn off. Is there any way to stop that?
- When I found the package name in my database what I'm doing is taking the user on the main screen "Home Page". That is not a good way to do it, can someone help me with this one. I need to properly block that app.
- Is there any way to close the application which I am trying to block from recent apps?
My code is listed below:
Accessibility service class
override fun onServiceConnected() {
super.onServiceConnected()
val info = serviceInfo
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
info.flags = AccessibilityServiceInfo.DEFAULT
info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
info.flags = AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS
info.flags = AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY
info.flags = AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
info.notificationTimeout = 100
info.packageNames = null
serviceInfo = info
}
override fun onAccessibilityEvent(event: AccessibilityEvent) {
if(AccessibilityEvent.eventTypeToString(event.eventType).contains("WINDOW")){
val nodeInfo = event.source
dfs(nodeInfo)
}
if (event.packageName != null) {
getAllBlockedApps(packageName = event.packageName.toString())
return
}
}
Block Application Code
val startMain = Intent(Intent.ACTION_MAIN)
startMain.addCategory(Intent.CATEGORY_HOME)
startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startMain.flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
startActivity(startMain)