11

My application allows launching of other 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 collapse every time and code running always).

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 status bar only if user try to expand this.Is there any intent or intent filter exist for detect expanding of Status bar?

Thanks in Advance

IgorGanapolsky
  • 24,768
  • 21
  • 112
  • 140
Devu Soman
  • 2,208
  • 13
  • 34
  • 56
  • 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
  • 3,365
  • 2
  • 23
  • 40
Lalith B
  • 11,413
  • 5
  • 28
  • 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
  • 80,914
  • 21
  • 183
  • 190
  • 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