5

I found this way to expanded Notifications Panel

Object sbservice = getSystemService("statusbar");
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(sbservice);

After looking into source, I also found collapsePanels method to collapse the notification

But I cannot find any way to detect the notification panel status

Because I want to check whether it is opened or closed, and then decide I should open it or close it

How can I know this status?

CL So
  • 3,647
  • 10
  • 51
  • 95
  • 1
    Possible duplicate of [How to tell if notification shade is pulled down in android](http://stackoverflow.com/questions/3161458/how-to-tell-if-notification-shade-is-pulled-down-in-android) – Tim Oct 22 '15 at 09:49
  • 1
    Also read http://stackoverflow.com/questions/11323637/why-cant-i-find-a-reference-to-the-statusbarmanager-class-directly. Using the hidden statusbarmanager in your app might not be the greatest of ideas – Tim Oct 22 '15 at 09:50
  • 1
    Since the code is in service, so it is impossible track by activity. And this is a very important function in my app, And the reflection is the only way, so I need use any possible method to implement it – CL So Oct 22 '15 at 09:58
  • I understand. You can still use the solution in that link, but you need to have some way to inform the service that the app is active/inactive – Tim Oct 22 '15 at 09:59
  • ~"**to expanded Notifications Panel**" Are you saying to detect notification panel status, or to expand it? – IgorGanapolsky May 03 '16 at 13:45
  • 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:45

1 Answers1

0

Though it's very late but it might help someone

Refer to this

You can use below method for detecting the notification panel pull,

In your manifest file

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

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

This uses the permission

@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();
    }
} }
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122