[ACCEPTED]-How can I programmatically open/close notifications in Android?-android-notification-bar

Accepted answer
Score: 48

You can programmatically close the notification 9 drawer by broadcasting an ACTION_CLOSE_SYSTEM_DIALOGS intent.

This causes 8 "temporary system dialogs" to 7 be dismissed. From the documentation:

Some 6 examples of temporary system dialogs are 5 the notification window-shade and the recent 4 tasks dialog.

This doesn't require any permissions, and 3 has apparently been available since Android 2 1.0.

The following code works for me on a 1 Nexus 4 running Android 5.0:

Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Score: 26

The answer from Ashwin works for Android versions below 4.2.2 10 (i.e. below version 17). In 4.2.2, the 9 "expand" method was changed to 8 "expandNotificationsPanel". If 7 you don't use that method name for 4.2.2 6 and above, you will get a Null Pointer Exception. So 5 the code should be:

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 );

And appropriate permission 4 should be added to AndroidManifest.

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

Obviously, this is not 3 part of the published API, so this is not 2 guaranteed to work in the future and many 1 people would advise against doing this.

Score: 15

Yes you can add this code to wherever you 1 want it to execute

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

And add this permission

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

How can I programmatically open/close notifications 15 in Android?

What you want cannot be done 14 using the Android SDK.

I know it's possible 13 because all the cuustome launchers are able 12 to do it via a button press (LauncherPro, ADW, etc).

All 11 of the "custom launchers" are bypassing 10 the SDK, using a variation on the technique 9 that @Yoni Samlan proposed in another answer 8 to your question. Things that are not part 7 of the SDK can be removed by device manufacturers, replaced 6 by the core Android team in future releases, etc.

I 5 would argue that what you want should be possible 4 via the SDK; otherwise, it really limits 3 alternative home screen implementations. However, what 2 you and I want does not count for all that 1 much.

Score: 4

Sadly there's still no official API (requested 3 here), but for now, you can use this code, which 2 I've generalized from all of the answers 1 I've found :

// based on https://gist.github.com/XinyueZ/7bad2c02be425b350b7f 
// requires permission: "android.permission.EXPAND_STATUS_BAR"
@SuppressLint("WrongConstant", "PrivateApi")
fun setExpandNotificationDrawer(context: Context, expand: Boolean) {
    try {
        val statusBarService = context.getSystemService("statusbar")
        val methodName =
                if (expand)
                    if (Build.VERSION.SDK_INT >= 17) "expandNotificationsPanel" else "expand"
                    if (Build.VERSION.SDK_INT >= 17) "collapsePanels" else "collapse"
        val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
        val method: Method = statusBarManager.getMethod(methodName)
    } catch (e: Exception) {

More Related questions