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
Christopher Orr's answer works great for closing the notification drawer.
You can use AccessibilityServices to programmatically show the notifications or quick settings by calling:
(
GLOBAL_ACTION_NOTIFICATIONS)
(
GLOBAL_ACTION_QUICK_SETTINGS)
Yes you can add this code to wherever you 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" />
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);
Here's the showNotifications method from Android's Launcher
:
private void showNotifications() {
final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
if (statusBar != null) {
statusBar.expand();
}
}
(which, as Robby indicated, is the "statusbar" system service).
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.
Sadly there's still no official API (requested here), but for now, you can use this code, which I've generalized from all of the answers 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"
else
if (Build.VERSION.SDK_INT >= 17) "collapsePanels" else "collapse"
val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
val method: Method = statusBarManager.getMethod(methodName)
method.invoke(statusBarService)
} catch (e: Exception) {
e.printStackTrace()
}
}