如何在 Android 中以编程方式打开/关闭通知?

发布于 2024-10-17 14:39:39 字数 111 浏览 8 评论 0原文

我到处搜索,但在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

余生再见 2024-10-24 14:39:39

您可以通过广播 以编程方式关闭通知抽屉ACTION_CLOSE_SYSTEM_DIALOGS 意图。

这会导致“临时系统对话框”被关闭。从文档中:

临时系统对话框的一些示例是通知窗口阴影和最近的任务对话框。

这不需要任何权限,并且显然自 Android 1.0 以来就可用。

以下代码适用于运行 Android 5.0 的 Nexus 4:

Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);

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);
黑白记忆 2024-10-24 14:39:39

Ashwin 的回答适用于 4.2.2 以下的 Android 版本(即版本 17 以下)。在4.2.2中,“expand”方法更改为“expandNotificationsPanel”。如果您在 4.2.2 及更高版本中不使用该方法名称,您将收到空指针异常。所以代码应该是:

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

并且应该在AndroidManifest中添加适当的权限。

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

显然,这不是已发布 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:

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.

尽揽少女心 2024-10-24 14:39:39

是的,您可以将此代码添加到您希望其执行的任何位置

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

并添加此权限

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

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" />
懵少女 2024-10-24 14:39:39

遗憾的是,仍然没有官方 API(此处请求,请考虑加注星标),但现在,您可以使用此代码,我从我找到的所有答案中概括了该代码:

// 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()
    }
}

编辑:还有 adb 命令,这可能是一种替代解决方法(取自 此处):

折叠:

adb shell service call statusbar 2

展开:

adb shell service call statusbar 1

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 :

// 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()
    }
}

EDIT: also adb command, which might be an alternative workaround (taken from here) :

collapse:

adb shell service call statusbar 2

expand:

adb shell service call statusbar 1
真心难拥有 2024-10-24 14:39:39

如何在 Android 中以编程方式打开/关闭通知?

使用 Android SDK 无法完成您想要的操作。

我知道这是可能的,因为所有自定义启动器都可以通过按下按钮来完成(LauncherPro、ADW 等)。

所有“自定义启动器”都绕过 SDK,使用 @Yoni Samlan 在另一个问题答案中提出的技术变体。设备制造商可以删除不属于 SDK 的内容,由核心 Android 团队在未来的版本中替换等等。

我认为您想要的东西应该可以通过 SDK 实现;否则,它确实限制了替代主屏幕的实现。然而,你我想要的并没有那么重要。

How can I programmatically open/close notifications in Android?

What you want cannot be done using the Android SDK.

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

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.

山田美奈子 2024-10-24 14:39:39

Christopher Orr 的答案非常适合关闭通知抽屉

您可以使用 AccessibilityServices 通过调用以下方式以编程方式显示通知或快速设置:

微暖i 2024-10-24 14:39:39

下面是来自 Android 的 Launcher 的 showNotifications 方法:(

private void showNotifications() {
    final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
    if (statusBar != null) {
        statusBar.expand();
    }
}

正如 Robby 所指出的,它是“状态栏”系统服务)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文