2013-03-22 55 views
15

我浏览过Stackoverflow并看到这个问题已被询问,但我没有找到任何解决方案。如何在通知按钮点击后关闭状态栏/通知面板

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

这里是我的通知码:

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

http://stackoverflow.com/a/6435373/115145按钮的支持,这是你的'minSdkVersion',等你应该没问题,但我不知道如何关闭抽屉。我对我的错误表示歉意 - 我忘记了他们正式开始支持这一点。 – CommonsWare 2013-03-22 13:03:10

+0

Np,也许我应该提前一点说我的minSdkVersion = 11 ;-) – 2013-03-22 13:17:30

回答

1

好,我找到了解决办法。这不是公共的API,但它的作品(目前):

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"/> 

需要

+0

这里的c是什么?这个代码会做什么? – 2013-06-04 12:22:14

+0

@iDroidExplorer:c是上下文。而且,正如我的问题所问,这段代码能够关闭扩展的通知栏面板。我使用这段代码后,我按了我的自定义通知按钮 – 2013-06-04 13:17:08

+1

更完整的答案http://stackoverflow.com/a/16978341/506724 – falko 2015-07-23 21:26:36

74

使用下面的两个代码行崩溃通知栏。作为API级别11.你在我现在删除的回答您的评论指出

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
context.sendBroadcast(it); 
+1

这是正确的答案,因为此代码无私有API的工作。 – CeccoCQ 2014-08-21 07:56:26

+0

你节省了我的时间。谢谢。 – BSKANIA 2014-11-19 12:44:35

+2

这应该被标记为答案.... – prince 2014-11-21 09:10:48