如何在 Android 中以编程方式打开/关闭通知?
我到处搜索,但在 SDK 或 Google 上找不到任何关于如何执行此操作的信息。我知道这是可能的,因为所有自定义启动器都可以通过按下按钮(LauncherPro、ADW 等)来完成。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过广播 以编程方式关闭通知抽屉
ACTION_CLOSE_SYSTEM_DIALOGS
意图。这会导致“临时系统对话框”被关闭。从文档中:
这不需要任何权限,并且显然自 Android 1.0 以来就可用。
以下代码适用于运行 Android 5.0 的 Nexus 4:
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:
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:
Ashwin 的回答适用于 4.2.2 以下的 Android 版本(即版本 17 以下)。在4.2.2中,“expand”方法更改为“expandNotificationsPanel”。如果您在 4.2.2 及更高版本中不使用该方法名称,您将收到空指针异常。所以代码应该是:
并且应该在
AndroidManifest
中添加适当的权限。显然,这不是已发布 API 的一部分,因此不能保证将来可以正常工作,许多人建议不要这样做。
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:
And appropriate permission should be added to
AndroidManifest
.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.
是的,您可以将此代码添加到您希望其执行的任何位置
并添加此权限
Yes you can add this code to wherever you want it to execute
And add this permission
遗憾的是,仍然没有官方 API(此处请求,请考虑加注星标),但现在,您可以使用此代码,我从我找到的所有答案中概括了该代码:
编辑:还有 adb 命令,这可能是一种替代解决方法(取自 此处):
折叠:
展开:
Sadly there's still no official API (requested here, please consider starring), but for now, you can use this code, which I've generalized from all of the answers I've found :
EDIT: also adb command, which might be an alternative workaround (taken from here) :
collapse:
expand:
使用 Android SDK 无法完成您想要的操作。
所有“自定义启动器”都绕过 SDK,使用 @Yoni Samlan 在另一个问题答案中提出的技术变体。设备制造商可以删除不属于 SDK 的内容,由核心 Android 团队在未来的版本中替换等等。
我认为您想要的东西应该可以通过 SDK 实现;否则,它确实限制了替代主屏幕的实现。然而,你我想要的并没有那么重要。
What you want cannot be done using the Android SDK.
All of the "custom launchers" are bypassing the SDK, using a variation on the technique that @Yoni Samlan proposed in another answer to your question. Things that are not part of the SDK can be removed by device manufacturers, replaced by the core Android team in future releases, etc.
I would argue that what you want should be possible via the SDK; otherwise, it really limits alternative home screen implementations. However, what you and I want does not count for all that much.
Christopher Orr 的答案非常适合关闭通知抽屉。
您可以使用 AccessibilityServices 通过调用以下方式以编程方式显示通知或快速设置:
performGlobalAction
(
GLOBAL_ACTION_NOTIFICATIONS
)
PerformGlobalAction
(
GLOBAL_ACTION_QUICK_SETTINGS
)
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:
performGlobalAction
(
GLOBAL_ACTION_NOTIFICATIONS
)
performGlobalAction
(
GLOBAL_ACTION_QUICK_SETTINGS
)
下面是来自 Android 的
Launcher
的 showNotifications 方法:(正如 Robby 所指出的,它是“状态栏”系统服务)。
Here's the showNotifications method from Android's
Launcher
:(which, as Robby indicated, is the "statusbar" system service).