OVERVIEW :
Recently, I came across an issue where the reflections used for collapsing status bar were not working from Android 12. When I tried to find alternative solutions, I got one using Accessibility Service in Android.
Reflections used for collapsing status bar :
// Prior to API 17, the method to call is 'collapse()'
// API 17 onwards, the method to call is `collapsePanels()`
Object sbservice = context.getSystemService("statusbar");
try {
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapseStatusBar = null;
if (Build.VERSION.SDK_INT <= 16) {
collapseStatusBar = statusbarManager.getMethod("collapse");
} else {
collapseStatusBar = statusbarManager.getMethod("collapsePanels");
}
collapseStatusBar.setAccessible(true);
collapseStatusBar.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
statusBarManager.getMethod(“collapsePanels”); is not working from API 31 since it is hidden.
Alternative solution for preventing Status Bar Expansion from API 31 & above using Accessibility Service :
We can use AccessibilityService.GLOBAL_ACTION_DISMISS_NOTIFICATION_SHADE to prevent Status Bar Expansion when pulled down.
/*** Dismisses the notification panel when pulled down ***/
public boolean dismissNotificationShade() {
try {
if(Build.VERSION.SDK_INT >= 31) {
return performGlobalAction(GLOBAL_ACTION_DISMISS_NOTIFICATION_SHADE);
}
}
catch (Exception e){
e.printStackTrace();
}
return false;
}
AccessibilityService class must be extended in order to use the above method.
Note : This method should be called continuously in the background thread. If the status/notification bar is pulled down, then this method will dismiss the bar and return true, otherwise false will be returned.
Hope it resolves your issue.
Thanks for reading,
Cheers!
no funciona con la version 12 estoy ejecutandolo en mi MyAccessibilityService por cada deteccion que hay lo esta ejecutando pero no hace efecto