Tech Blog

Android

How to prevent Status Bar Expansion in Android 12 & above devices?

By Ankur Agrawal1 min read
← Back to blog

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!

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →