11

My application allows launching of another application from mine. None of my activity shows Status Bar. But when launching other applications like Camera the user can access the status bar. So I tried the following code snippet for collapsing the Status Bar inside a service (so it collapses every time and the code is always running).

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapse = null;
if(currentapiVersion <= 16){
    collapse = statusbarManager.getMethod("collapse");
}else{
    collapse = statusbarManager.getMethod("collapsePanels");
}
collapse.setAccessible(true);
collapse.invoke(service);

Now I want to collapse the status bar only if the user tries to expand this? Is there any intent or intent filter that exists for detect expanding of the Status bar?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Devu Soman
  • 2,246
  • 13
  • 36
  • 57
  • Check this for a working solution: https://stackoverflow.com/questions/53509108/how-to-detect-when-the-notification-system-bar-is-opened/53509109#53509109 – The Hungry Androider Nov 28 '18 at 15:43

2 Answers2

15

In your activity override the onWindowFocusChanged() method and write the below code.

This uses the following permission:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    try
    {
        if(!hasFocus)
        {
            Object service  = getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse .setAccessible(true);
            collapse .invoke(service);
        }
    }
    catch(Exception ex)
    {
        if(!hasFocus)
        {
            try {
                Object service  = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse .setAccessible(true);
                collapse .invoke(service);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();                
            }
            ex.printStackTrace();
        }
    }
}
Pedro Massango
  • 4,114
  • 2
  • 28
  • 48
Lalith B
  • 11,843
  • 6
  • 29
  • 47
  • 1
    I can't use onWindowFocusChanged().Because this focus only my activity and I specified in the question that it is possible to launch other apps from mine.Then this won't work. – Devu Soman Apr 30 '13 at 10:27
  • you can collapse the status bar if your window has some other activity other than the launcher. you can query the packagemanager and get the top activity. if the top activity is not Launcher then you have to collapse the statusbar. – Lalith B May 02 '13 at 04:07
  • @LalithB I've `RemoteViews` based notification, On opening the notification-panel and swiping down I can change the notification view from `smallContentView` to `BigContentView`. But I want my notification to go back to `SmallContentView` once the notification-panel is closed(swiped up). Is it possible? – Vivek Kumar Mar 15 '16 at 15:53
  • That would be another question all together, and yes that can be handled in the remote views itself. – Lalith B Mar 15 '16 at 16:38
  • NoSuchMethod as "collapse" – G. Ciardini Sep 06 '21 at 13:52
7

There is no callback of any kind when the notification bar is dragged down on Android.

This is because Android apps are meant to be designed in a way that the notification bar coming up and going away does not affect the functioning in any way.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • I've `RemoteViews` based notification, On opening the notification-panel and swiping down I can change the notification view from `smallContentView` to `BigContentView`. But I want my notification to go back to `SmallContentView` once the notification-panel is closed(swiped up). Is it possible? – Vivek Kumar Mar 15 '16 at 15:53
  • @dearvivekkumar How you can detect the opening the notification-panel and swiping down? – hasnain_ahmad Jul 27 '16 at 07:27