Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically open/close notifications in Android?

I've searched everywhere, but can't find anything in the SDK or on Google on how to do this. I know it's possible because all the custom launchers are able to do it via a button press (LauncherPro, ADW, etc).

Thanks.

like image 787
user496854 Avatar asked Feb 17 '11 13:02

user496854


People also ask

How do I open the notification drawer?

The Notification Panel is at the top of your mobile device's screen. It is hidden in the screen but can be accessed by swiping your finger from the top of the screen to the bottom.


2 Answers

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

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

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

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

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

Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); context.sendBroadcast(closeIntent); 
like image 118
Christopher Orr Avatar answered Oct 26 '22 17:10

Christopher Orr


The answer from Ashwin works for Android versions below 4.2.2 (i.e. below version 17). In 4.2.2, the "expand" method was changed to "expandNotificationsPanel". If you don't use that method name for 4.2.2 and above, you will get a Null Pointer Exception. So 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 should be added to AndroidManifest.

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

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

like image 29
Jonathan Avatar answered Oct 26 '22 17:10

Jonathan