单击通知按钮后如何关闭状态栏/通知面板 [英] How to close the status bar/notification panel after notification button click

查看:17
本文介绍了单击通知按钮后如何关闭状态栏/通知面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了 Stackoverflow,看到有人问过这个问题,但没有找到任何解决方案.

I've browsed through Stackoverflow and have seen that this question has been asked, but I didn't find any solution.

我有一个带有 2 个按钮的自定义通知.我希望在按下该通知上的按钮后关闭状态栏面板,但不知道如何关闭.如果我按下通知本身(所以按下 contentIntent),面板就会关闭,这也是我想要的按钮.

I have a custom notification with 2 buttons. I want the status bar panel to close after I press a button on that notification, but don't know how. If I press the notification itself (so the contentIntent), then the panel closes, which is what I also want for my buttons.

这是我的通知代码:

Notification notification = new Notification(R.drawable.icon, "Service running", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.contentIntent = openIntent;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);


contentView.setOnClickPendingIntent(R.id.button1, stopIntent);
contentView.setOnClickPendingIntent(R.id.button2, addIntent);

notification.contentView = contentView;

notificationManager.notify(NOTIFICATION_ID, notification);

推荐答案

好的,我找到了解决方案.它不是公共 API,但它有效(目前):

Ok I've found the solution. It's not public API, but it works (for the moment):

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

为了这个工作

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

需要

这篇关于单击通知按钮后如何关闭状态栏/通知面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆