6

I know I can expand the notification bar by reflection

    Class<?> statusbarManager = Class.forName( "android.app.StatusBarManager" );
    Method showsb;
    if (Build.VERSION.SDK_INT >= 17) {
        showsb = statusbarManager.getMethod("expandNotificationsPanel");
    }
    else {
        showsb = statusbarManager.getMethod("expand");
    }
    showsb.invoke( getSystemService( "statusbar" ) );

However, is there a way to expand it if it is collapsed, and collapse it if it is already expanded?

There is a toggle function for StatusBarManager in the android docs but it doesn't work for me.

EDITED

I am calling this function from inside a bound service.

daniely
  • 7,313
  • 5
  • 29
  • 46
  • Daniel, sorry that my (wrong) answer ended up taking the bounty here. I've been looking around and I haven't been able to find an answer to this anywhere online. I'll keep looking though! – Alex K Nov 30 '14 at 03:55
  • There are no public API to do what you want. Even if you use reflection, it's still restricted- as you have found out. –  Jul 09 '15 at 16:18

2 Answers2

3

I think that you may be missing the needed permissions. Make sure that you have the EXPAND_STATUS_BAR permission in your Manifest.xml:

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

Looking around, I found this code:

Object sbservice = getSystemService( "statusbar" );
Class<?> statusbarManager = Class.forName( "android.app.StatusBarManager" );
Method showsb = statusbarManager.getMethod( "expand" );
showsb.invoke( sbservice );

You may want to try it.

EDIT:

For actually detecting if it is down or not, see this answer. Here's what it looks like:

Override the onWindowFocusChanged() method in your activity with the code below:

In the permissions:

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

The overriding:

@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();
        }
    }
}
Community
  • 1
  • 1
Alex K
  • 8,269
  • 9
  • 39
  • 57
  • I already have the correct permissions and can expand the bar successfully. However, I need a way to know if the bar is already expanded or not. – daniely Nov 21 '14 at 02:31
  • 1
    Thanks Alex, I am actually calling this function from a bound service. So I could be viewing any app while this code is triggered. I've updated the original question, sorry about the missing details. – daniely Nov 21 '14 at 03:01
  • This is only applicable for your own app, but is it possible we can detect any statusbar expand at any time in the lifetime of the app? means can I implement these in the service kind of thing and we can implement these also on android lock screen. – Sanat Pandey Sep 05 '16 at 05:42
0

The below code will work for new and old Android OS.

You need to enable the EXPAND_STATUS_BAR permission in AndroidManifest.xml.

try {
    Object sbservice = getSystemService("statusbar");
    Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
    Method expandMethod;
    expandMethod = statusbarManager.getMethod(Build.VERSION.SDK_INT >= 17 ? "expandNotificationsPanel" : "expand");
    expandMethod.invoke(sbservice);
} catch (Exception ex) {
    ex.printStackTrace();
}
Googlian
  • 6,077
  • 3
  • 38
  • 44